日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SDWebImage 4 0 迁移指南

發(fā)布時(shí)間:2023/12/6 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SDWebImage 4 0 迁移指南 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

剛剛更新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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。