SDWebImage 4 0 迁移指南
剛剛更新pods 編譯程序,突然發(fā)現(xiàn)SDWebImage報(bào)錯(cuò)
了解到SDWebImage4.0 更換了不少方法,還增加了幾個(gè)類,索性都研究一下- pod 更新SDWebImage版本為4.1.0
查找是否有對應(yīng)緩存的 方法 由返回BOOL 值 換成Block回調(diào)中參數(shù)返回BOOL值
//老版本 BOOL isInCache =[[SDImageCache sharedImageCache]diskImageExistsWithKey:@""];// 4.0 版本[[SDImageCache sharedImageCache]diskImageExistsWithKey:@"" completion:^(BOOL isInCache) {}]; 復(fù)制代碼刪除沙盒圖片 只有 帶Block回調(diào)的了
老版本 - (void)clearMemory; - (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion; - (void)clearDisk; 4.0 版本 - (void)clearMemory; - (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion; 復(fù)制代碼下載圖片的方法 方法名字由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) { }]; 復(fù)制代碼UIImageView+WebCache UIButton+WebCache等category 的方法并沒改變
只是里面的圖片替換邏輯 移動到新增的UIView +WebCache 里了
- (void)sd_setImageWithURL:(nullable NSURL *)urlplaceholderImage:(nullable UIImage *)placeholderoptions:(SDWebImageOptions)optionsprogress:(nullable SDWebImageDownloaderProgressBlock)progressBlockcompleted:(nullable SDExternalCompletionBlock)completedBlock {[self sd_internalSetImageWithURL:urlplaceholderImage:placeholderoptions:optionsoperationKey:nilsetImageBlock:nilprogress:progressBlockcompleted:completedBlock]; } 復(fù)制代碼增加了個(gè)UIView +WebCache category
此類作用是把SDWebImage 所有的 category 如 UIButton+WebCache,UIImageView+WebCache 等分類的圖片替換的邏輯 封裝到 一起使用。
- (void)sd_internalSetImageWithURL:(nullable NSURL *)urlplaceholderImage:(nullable UIImage *)placeholderoptions:(SDWebImageOptions)optionsoperationKey:(nullable NSString *)operationKeysetImageBlock:(nullable SDSetImageBlock)setImageBlockprogress:(nullable SDWebImageDownloaderProgressBlock)progressBlockcompleted:(nullable SDExternalCompletionBlock)completedBlock; 復(fù)制代碼SDWebImage4.0 播放GIF 需要使用 FLAnimatedImage
如果使用了用pods 除了pod 'SDWebImage' 還要添加下面?zhèn)z個(gè) pod 'SDWebImage/GIF' pod 'FLAnimatedImage'
//使用很簡單 #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];復(fù)制代碼FLAnimatedImageView+WebCache 內(nèi)部實(shí)行也很簡單明了, 依然調(diào)用UIView +WebCache 的方法 在設(shè)置圖片的Block里 對回調(diào)里的參數(shù)imageData 進(jìn)行判斷如果是GIF 就使用FLAnimatedImage 的animatedImageWithGIFData方法進(jìn)行賦值
- (void)sd_setImageWithURL:(nullable NSURL *)urlplaceholderImage:(nullable UIImage *)placeholderoptions:(SDWebImageOptions)optionsprogress:(nullable SDWebImageDownloaderProgressBlock)progressBlockcompleted:(nullable SDExternalCompletionBlock)completedBlock {__weak typeof(self)weakSelf = self;[self sd_internalSetImageWithURL:urlplaceholderImage:placeholderoptions:optionsoperationKey:nilsetImageBlock:^(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:progressBlockcompleted:completedBlock]; }復(fù)制代碼FLAnimatedImageView本身就是UIImageView的封裝 ,用起來真的很快,配合SDWebImage加載GIF 都感覺不到下載的過程,以為是本地加載的一樣 .在正常UITableView 列表試了下加載普通圖片更是小意思。完全可以替換UIImageView
增加了一個(gè)SDImageCacheConfig 類 把之前在SDImageCache 類里初始化的幾個(gè)屬性拿出來單獨(dú)封裝,使其他類也能使用
增加了 NSImage+WebCache category macOS 平臺開發(fā)時(shí)使用的一個(gè) NSImage 的一個(gè)分類 不用太了解
我也只是 粗略的看了看 ,具體還有哪些改變,漏掉的,以后再補(bǔ)充
總結(jié)
以上是生活随笔為你收集整理的SDWebImage 4 0 迁移指南的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server Update 所有
- 下一篇: iOS 玩转CocoaPods