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 *)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];
}
复制代码

增加了个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;
复制代码

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 *)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的封装 ,用起来真的很快,配合SDWebImage加载GIF 都感觉不到下载的过程,以为是本地加载的一样 .在正常UITableView 列表试了下加载普通图片更是小意思。完全可以替换UIImageView

增加了一个SDImageCacheConfig 类 把之前在SDImageCache 类里初始化的几个属性拿出来单独封装,使其他类也能使用

增加了 NSImage+WebCache category macOS 平台开发时使用的一个 NSImage 的一个分类 不用太了解

我也只是 粗略的看了看 ,具体还有哪些改变,漏掉的,以后再补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值