title: 2018-2-27 SDWebImage
SDWebImage 的使用
SDWebImage 概論
1.提供了一個UIImageView的category用來加載網(wǎng)絡圖片并且對網(wǎng)絡圖片的緩存進行管理
2.采用異步方式,使用 memory+disk 來緩存網(wǎng)絡圖片,自動管理緩存。
3.耗時操作都在子線程,確保不會阻塞主線程
SDWebImage 使用
1.使用UImageView+WebCache category來加載UITableView中cell的圖片
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imgUrlStr] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
2.使用Blocks,采用這個方案可以在網(wǎng)絡圖片加載過程中得知圖片的下載進度和圖片加載成功與否
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//... completion code here ...
}];
3.使用SDWebImageManager,SDWebImageManager為UIImageView+WebCache category的實現(xiàn)提供接口。
SDWebImageManager *manager = [SDWebImageManager sharedManager] ;
[manager downloadImageWithURL:imageURL options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
// do something with image
}
}];
SDWebImage 的流程圖

656644-7dfe370a86e157e7
SDWebImage 接口
1.所有使用 SDWebImage 的方法 都要調(diào)用下邊的方法
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock
2.取圖片的緩存大小
[SDImageCache sharedImageCache] getSize];
3.清理內(nèi)存,磁盤緩存
[[SDImageCache sharedImageCache] clearMemory];
[[SDImageCache sharedImageCache] clearMemory];
PS:第一次開始用 Markdown 寫文章,有問題請指正