SDWebImage 4 0 迁移指南
剛剛更新pods 編譯程序,突然發現SDWebImage報錯
了解到SDWebImage4.0 更換了不少方法,還增加了幾個類,索性都研究一下- pod 更新SDWebImage版本為4.1.0
查找是否有對應緩存的 方法 由返回BOOL 值 換成Block回調中參數返回BOOL值
//老版本 BOOL isInCache =[[SDImageCache sharedImageCache]diskImageExistsWithKey:@""];// 4.0 版本[[SDImageCache sharedImageCache]diskImageExistsWithKey:@"" completion:^(BOOL isInCache) {}]; 復制代碼刪除沙盒圖片 只有 帶Block回調的了
老版本 - (void)clearMemory; - (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion; - (void)clearDisk; 4.0 版本 - (void)clearMemory; - (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion; 復制代碼下載圖片的方法 方法名字由downloadImageWithURL 換成loadImageWithURL
1.在加載進度的Block回調里 增加了targetURL (圖片URL的參數) 2.在下載完成的Block回調里 增加了 data (返回二進制)
//老版本[[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 的方法并沒改變
只是里面的圖片替換邏輯 移動到新增的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]; } 復制代碼增加了個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; 復制代碼SDWebImage4.0 播放GIF 需要使用 FLAnimatedImage
如果使用了用pods 除了pod 'SDWebImage' 還要添加下面倆個 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];復制代碼FLAnimatedImageView+WebCache 內部實行也很簡單明了, 依然調用UIView +WebCache 的方法 在設置圖片的Block里 對回調里的參數imageData 進行判斷如果是GIF 就使用FLAnimatedImage 的animatedImageWithGIFData方法進行賦值
- (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]; }復制代碼FLAnimatedImageView本身就是UIImageView的封裝 ,用起來真的很快,配合SDWebImage加載GIF 都感覺不到下載的過程,以為是本地加載的一樣 .在正常UITableView 列表試了下加載普通圖片更是小意思。完全可以替換UIImageView
增加了一個SDImageCacheConfig 類 把之前在SDImageCache 類里初始化的幾個屬性拿出來單獨封裝,使其他類也能使用
增加了 NSImage+WebCache category macOS 平臺開發時使用的一個 NSImage 的一個分類 不用太了解
我也只是 粗略的看了看 ,具體還有哪些改變,漏掉的,以后再補充
總結
以上是生活随笔為你收集整理的SDWebImage 4 0 迁移指南的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server Update 所有
- 下一篇: iOS 玩转CocoaPods