剛剛更新pods 編譯程序,突然發(fā)現(xiàn)SDWebImage報(bào)錯(cuò)
5CFC472F-4DFD-4936-87B3-B47E94A87EC4.png
了解到SDWebImage4.0 更換了不少方法,還增加了幾個(gè)類,索性都研究一下
-
pod 更新SDWebImage版本為4.1.0
38BDB086-B1E2-469E-8681-415EF14DC2BF.png SDWebImage4.0 播放GIF 需要使用 FLAnimatedImage
如果使用了用pods 除了pod 'SDWebImage' 還要添加下面?zhèn)z個(gè)
pod 'SDWebImage/GIF'
pod 'FLAnimatedImage'
4FCC6433-223B-428C-A89F-7E2F8F04A6D8.png
//使用很簡(jiǎn)單
#import <FLAnimatedImageView.h>
#import <FLAnimatedImageView+WebCache.h>
FLAnimatedImageView *FLView = [[FLAnimatedImageView alloc]init];
FLView.frame = CGRectMake(0, 64, SCREEN_WIDTH, 280);
[FLView sd_setImageWithURL:[NSURL URLWithString:IMAGE2] placeholderImage:[UIImage imageNamed:[NSBundle zb_placeholder]]];
[self.view addSubview:FLView];
FLAnimatedImageView+WebCache 內(nèi)部實(shí)行也很簡(jiǎn)單明了, 依然調(diào)用UIView +WebCache 的方法 在設(shè)置圖片的Block里 對(duì)回調(diào)里的參數(shù)imageData 進(jìn)行判斷如果是GIF 就使用FLAnimatedImage 的animatedImageWithGIFData方法進(jìn)行賦值
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDExternalCompletionBlock)completedBlock {
__weak typeof(self)weakSelf = self;
[self sd_internalSetImageWithURL:url
placeholderImage:placeholder
options:options
operationKey:nil
setImageBlock:^(UIImage *image, NSData *imageData) {
SDImageFormat imageFormat = [NSData sd_imageFormatForImageData:imageData];
if (imageFormat == SDImageFormatGIF) {
weakSelf.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
weakSelf.image = nil;
} else {
weakSelf.image = image;
weakSelf.animatedImage = nil;
}
}
progress:progressBlock
completed:completedBlock];
}
FLAnimatedImageView本身就是UIImageView的封裝 订讼,用起來(lái)真的很快擂煞,配合SDWebImage加載GIF 都感覺不到下載的過程华坦,以為是本地加載的一樣 .在正常UITableView 列表試了下加載普通圖片更是小意思昆咽。完全可以替換UIImageView
- 查找是否有對(duì)應(yīng)緩存的 方法 由返回BOOL 值 換成Block回調(diào)中參數(shù)返回BOOL值
//老版本
BOOL isInCache =[[SDImageCache sharedImageCache]diskImageExistsWithKey:@""];
// 4.0 版本
[[SDImageCache sharedImageCache]diskImageExistsWithKey:@"" completion:^(BOOL isInCache) {}];
- 刪除沙盒圖片 只有 帶Block回調(diào)的了
老版本
- (void)clearMemory;
- (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion;
- (void)clearDisk;
4.0 版本
- (void)clearMemory;
- (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion;
- 下載圖片的方法 方法名字由downloadImageWithURL 換成loadImageWithURL
1.在加載進(jìn)度的Block回調(diào)里 增加了targetURL (圖片URL的參數(shù))
2.在下載完成的Block回調(diào)里 增加了 data (返回二進(jìn)制)
//老版本
[[SDWebImageManager sharedManager]downloadImageWithURL:[NSURL URLWithString:@""] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
}];
//4.0 版本
[[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:[imageArray objectAtIndex:i]] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
}];
- UIImageView+WebCache UIButton+WebCache等category 的方法并沒改變
只是里面的圖片替換邏輯 移動(dòng)到新增的UIView +WebCache 里了
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDExternalCompletionBlock)completedBlock {
[self sd_internalSetImageWithURL:url
placeholderImage:placeholder
options:options
operationKey:nil
setImageBlock:nil
progress:progressBlock
completed:completedBlock];
}
- 增加了個(gè)UIView +WebCache category
此類作用是把SDWebImage 所有的 category 如 UIButton+WebCache,UIImageView+WebCache 等分類的圖片替換的邏輯 封裝到 一起使用铛碑。
- (void)sd_internalSetImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
operationKey:(nullable NSString *)operationKey
setImageBlock:(nullable SDSetImageBlock)setImageBlock
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDExternalCompletionBlock)completedBlock;
- 增加了一個(gè)SDImageCacheConfig 類 把之前在SDImageCache 類里初始化的幾個(gè)屬性拿出來(lái)單獨(dú)封裝,使其他類也能使用
- 增加了 NSImage+WebCache category macOS 平臺(tái)開發(fā)時(shí)使用的一個(gè) NSImage 的一個(gè)分類 不用太了解
我也只是 粗略的看了看 虽界,具體還有哪些改變汽烦,漏掉的,以后再補(bǔ)充