iOS开发实用技术人猿星球iOS学习笔记

SDWebImage 4.0 迁移指南

2017-09-15  本文已影响495人  _Andy_

刚刚更新pods 编译程序,突然发现SDWebImage报错

5CFC472F-4DFD-4936-87B3-B47E94A87EC4.png
了解到SDWebImage4.0 更换了不少方法,还增加了几个类,索性都研究一下 4FCC6433-223B-428C-A89F-7E2F8F04A6D8.png

//使用很简单

#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

//老版本
BOOL isInCache =[[SDImageCache sharedImageCache]diskImageExistsWithKey:@""];

// 4.0 版本
 [[SDImageCache sharedImageCache]diskImageExistsWithKey:@"" completion:^(BOOL isInCache) {}];
老版本
- (void)clearMemory;
- (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion;
- (void)clearDisk;
4.0 版本
- (void)clearMemory;
- (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion;
//老版本
 [[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) {
}];
- (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];
}
- (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;

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

上一篇 下一篇

猜你喜欢

热点阅读