SDWebImage源码学习-One

96
c_xiaoqiang
2015.07.13 19:45* 字数 634

源码地址< https://github.com/rs/SDWebImage >
SDWebImage提供UIImageView的category,支持从远程服务器下载及缓存图片资源

SDWebImage功能:

  • UIImageView的category增加了Web图片下载缓存操作
  • 一个异步的图片加载器
  • 一个异步的内存+磁盘缓存策略
  • GIF图片支持
  • 支持WebP格式的图片
  • 后台图片解压缩处理
  • 确保同一个URL地址不会被重复下载
  • 确保一个假冒的URL地址不会被重复的请求
  • 确保主线程不会被阻塞
  • 使用GCD和ARC
  • Arm64的支持

SDWebImageManager

可以通过SDWebImageManager去下载缓存图片,它将一个下载器(SDWebImageDownloader)和一个图片缓存器(SDImageCache)绑定在一起。经常会使用到的分类UIImageView+WebCache也是基于它实现的。下面是一个官方示例:

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:imageURL
             options:0
            progress:nil
           completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
               if (image) {
                   // do something with image
               }
           }];

SDWebImageManager绑定的下载器和图片缓存器都是只读性质,先看看在SDWebImageManager.h文件里面的定义是如何

@interface SDWebImageManager : NSObject

@property (weak, nonatomic) id <SDWebImageManagerDelegate> delegate;

@property (strong, nonatomic, readonly) SDImageCache *imageCache;
@property (strong, nonatomic, readonly) SDWebImageDownloader *imageDownloader;

首先有一个delegate,其声明了两个可选的方法

//选择控制哪个image该被下载,当发现image不在cache中的时候
- (BOOL)imageManager:(SDWebImageManager *)imageManager shouldDownloadImageForURL:(NSURL *)imageURL;
//允许对图片下载完后并且在存入缓存和磁盘前进行转换
- (UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL;

接下来看看SDWebImageManager主要下载图片的方法,返回值是一个遵循SDWebImageOperation协议类型的值

- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url
                                     options:(SDWebImageOptions)options
                                    progress:(SDWebImageDownloaderProgressBlock)progressBlock
                              completed:(SDWebImageCompletionWithFinishedBlock)completedBlock;

progressBlock在图片正在下载的时候进行处理,completedBlock当图片下载完成后进行的处理。上面已经有个小例子,下面是block的声明

typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize);//声明在SDWebImageDownloader.h文件中

typedef void(^SDWebImageCompletionWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL);

剩余的方法可以一并看看

- (void)saveImageToCache:(UIImage *)image forURL:(NSURL *)url;//保存图片到cache
- (void)cancelAll;//取消所有操作
- (BOOL)isRunning;//检查是否有下载图片操作在运行
- (BOOL)cachedImageExistsForURL:(NSURL *)url;//检查图片是否在cache中
- (BOOL)diskImageExistsForURL:(NSURL *)url;//检查图片是否在磁盘中
//检查图片是否在cache中,检查结束后进行block操作
- (void)cachedImageExistsForURL:(NSURL *)url
                 completion:(SDWebImageCheckCacheCompletionBlock)completionBlock;
//检查图片是否在磁盘中,检查结束后进行block操作
- (void)diskImageExistsForURL:(NSURL *)url
               completion:(SDWebImageCheckCacheCompletionBlock)completionBlock;
- (NSString *)cacheKeyForURL:(NSURL *)url;//根据url返回cache的key值

看完SDWebImageManager.h文件再看看SDWebImageManager.m文件里面一个遵循SDWebImageOperation协议的类SDWebImageCombinedOperation。

@interface SDWebImageCombinedOperation : NSObject <SDWebImageOperation>
//是否已经取消
@property (assign, nonatomic, getter = isCancelled) BOOL cancelled;
@property (copy, nonatomic) SDWebImageNoParamsBlock cancelBlock;
//真正用来控制下载的operation
@property (strong, nonatomic) NSOperation *cacheOperation;

@end

SDWebImageManager.m里面的大部分操作都是在下载图片的环节,通过SDImageCache和SDWebImageDownloader来实现。其它一些判断存在性的操作也很容易理解。

typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
//默认情况下,当URL下载失败,该URL会被列入黑名单,库就不会再去尝试获取该URL,该标记用来禁用黑名单
    SDWebImageRetryFailed = 1 << 0,
//默认情况下,图片下载是在UI交互的时候,该标记用来禁用这个情况,这样子下载会延迟到UIScrollView减速的时候
    SDWebImageLowPriority = 1 << 1,
//该标记禁用磁盘缓存
    SDWebImageCacheMemoryOnly = 1 << 2,
//该标记用来渐进式下载,如果浏览器一样,图片在下载过程中渐渐显示。默认情况下是下载完一次性显示
    SDWebImageProgressiveDownload = 1 << 3,
//即使图片已经缓存,也期望进行HTTP响应cache control并且如果有需要的话从远程地址更新图片数据
//磁盘缓存将被NSURLCache处理而不是SDWebImage,因为SDWebImage会导致轻微的性能下载。
//该标记帮助处理在请求同样的URL后面改变的图片。如果缓存图片被刷新,则完成的block会使用缓存图片再调用一次
    SDWebImageRefreshCached = 1 << 4,
//IOS4+,程序进入后台后仍然进行下载图片,请求系统给予额外的时间进行下载,如果请求超时了,操作就会被取消
    SDWebImageContinueInBackground = 1 << 5,
//通过设定NSMutableURLRequest.HTTPShouldHandleCookies = YES;来处理存储在NSHTTPCookieStore的cookies
    SDWebImageHandleCookies = 1 << 6,
//该标记允许不受信任的SSL认证
    SDWebImageAllowInvalidSSLCertificates = 1 << 7,
//默认情况下是按入队顺序下载,该标记可以让其优先下载
    SDWebImageHighPriority = 1 << 8
//默认情况下,占位图片在图片被加载时同时被加载,这个标记会让占位图片在图片加载完后再加载
    SDWebImageDelayPlaceholder = 1 << 9,
// 我们通常不调用动画图片的transformDownloadedImage代理方法,因为大多数转换代码可以使它变得糟糕。
// 使用这个标记则在任何情况下都进行转换。
 SDWebImageTransformAnimatedImage = 1 << 10,
};

SDImageCache

SD的缓存机制。首先来看看SDImageCache.h里面的一些声明。

//定义Cache类型
typedef NS_ENUM(NSInteger, SDImageCacheType) {
//不使用cache获得图片,依然会从web下载图片
    SDImageCacheTypeNone,
//图片从disk获得
    SDImageCacheTypeDisk,
//图片从Memory中获得  
    SDImageCacheTypeMemory
};

接下来是一些变量的声明

//这个变量默认值为YES,显示比较高质量的图片,但是会浪费比较多的内存,可以通过设置NO来缓解内存
@property (assign, nonatomic) BOOL shouldDecompressImages;
//总共的内存允许图片的消耗值
@property (assign, nonatomic) NSUInteger maxMemoryCost;
//图片存活于内存的时间初始化的时候默认为一周
@property (assign, nonatomic) NSInteger maxCacheAge;
//每次存储图片大小的限制
@property (assign, nonatomic) NSUInteger maxCacheSize;

看看SDImageCache的初始化

- (id)initWithNamespace:(NSString *)ns {
if ((self = [super init])) {
    NSString *fullNamespace = [@"com.hackemist.SDWebImageCache." stringByAppendingString:ns];

    // 初始化 PNG 的数据签名
    kPNGSignatureData = [NSData dataWithBytes:kPNGSignatureBytes length:8];

    // 创建IO队列
    _ioQueue = dispatch_queue_create("com.hackemist.SDWebImageCache", DISPATCH_QUEUE_SERIAL);

    //初始化清除缓存期限,默认一周
    _maxCacheAge = kDefaultCacheMaxCacheAge;

    // 初始化缓冲器
    _memCache = [[NSCache alloc] init];
    _memCache.name = fullNamespace;

    // 初始化磁盘缓存
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    _diskCachePath = [paths[0] stringByAppendingPathComponent:fullNamespace];

    // 设置显示高质量图片
    _shouldDecompressImages = YES;

    dispatch_sync(_ioQueue, ^{
        _fileManager = [NSFileManager new];
    });

#if TARGET_OS_IPHONE
    // 订阅通知事件
//内存不足的时候清除缓存
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(clearMemory)
                                                 name:UIApplicationDidReceiveMemoryWarningNotification
                                               object:nil];
//期限到的时候清除缓存
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(cleanDisk)
                                                 name:UIApplicationWillTerminateNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(backgroundCleanDisk)
                                                 name:UIApplicationDidEnterBackgroundNotification
                                               object:nil];
#endif
}
return self;}

SDImageCache中用来存储图片的方法:

- (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk {
    if (!image || !key) {
       return;
    }
//cost的值与maxCacheSize相关,如果大于这个值,则在缓存不足时会被清除
 [self.memCache setObject:image forKey:key cost:image.size.height * image.size.width * image.scale * image.scale];
  if (toDisk) {//图片是否存储到disk中
       dispatch_async(self.ioQueue, ^{
           NSData *data = imageData;
         if (image && (recalculate || !data)) {
#if TARGET_OS_IPHONE
             BOOL imageIsPng = YES;
             if ([imageData length] >= [kPNGSignatureData length]) {
                   imageIsPng = ImageDataHasPNGPreffix(imageData);
              }
    //根据图片格式,获取data数据
                if (imageIsPng) {
                    data = UIImagePNGRepresentation(image);
                }
                else {
                    data = UIImageJPEGRepresentation(image, (CGFloat)1.0);
            }
#else
                data = [NSBitmapImageRep representationOfImageRepsInArray:image.representations usingType: NSJPEGFileType properties:nil];
#endif
        }
            if (data) {
                if (![_fileManager fileExistsAtPath:_diskCachePath]) {
                    [_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];              
                  }
        //存储路径和数据
             [_fileManager createFileAtPath:[self defaultCachePathForKey:key] contents:data attributes:nil];
        }
    });
}
}

几个获取缓存和清除缓存接口

- (NSUInteger)getSize //获取磁盘缓存大小
- (NSUInteger)getDiskCount //获取缓存图片数量
- (void)clearMemory;//清除内存
- (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion;//清除缓存,不管到期与否,完成后操作
- (void)clearDisk;//清除缓存,不管到期与否
- (void)cleanDiskWithCompletionBlock:(SDWebImageNoParamsBlock)completionBlock;//清除到期缓存图片,完成后操作
- (void)cleanDisk;//清除到期缓存图片

来个示例代码实现

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager.imageCache setMaxMemoryCost:1000000];//设置总缓存大小,默认为0没有限制
[manager.imageCache setMaxCacheSize:640000];//设置单个图片限制大小
[manager.imageDownloader setMaxConcurrentDownloads:1];//设置同时下载线程数,这是下载器的内容,下面将会介绍
[manager downloadImageWithURL:[NSURL URLWithString:@"http://p9.qhimg.com/t01eb74a44c2eb43193.jpg"]
                      options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                          NSLog(@"%lu", receivedSize);
                      } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                          self.imageView1.image = image;
                          
                      }];
[manager downloadImageWithURL:[NSURL URLWithString:@"http://img.article.pchome.net/00/28/33/87/pic_lib/wm/kuanpin12.jpg"]
                      options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                          NSLog(@"%lu", receivedSize);
                      } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                          self.imageView2.image = image;
                          
                      }];
NSUInteger size = [manager.imageCache getSize];
NSUInteger count = [manager.imageCache getDiskCount];
NSLog(@"size = %lu", size); // 644621(两张测试图片)
NSLog(@"count = %lu", count); // 2
[manager.imageCache clearDisk];
size = [manager.imageCache getSize];
count = [manager.imageCache getDiskCount];
NSLog(@"sizeClean = %lu", size);  //  0
NSLog(@"countClean = %lu", count);     //  0   这里使用的是clear

SDImageCache中有根据键值对清除的方法

- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(SDWebImageNoParamsBlock)completion;

还有一些根据key查询图片的方法

- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock;
- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key;
- (UIImage *)imageFromDiskCacheForKey:(NSString *)key;

SDWebImageDownloader

SDWebImageDownloader.h里面的一些定义

//队列的下载方式
typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
    SDWebImageDownloaderFIFOExecutionOrder,//先进先出
    SDWebImageDownloaderLIFOExecutionOrder//后进先出
};

@property (assign, nonatomic) BOOL shouldDecompressImages;//与cache相同
@property (assign, nonatomic) NSInteger maxConcurrentDownloads;//最大下载线程数
@property (readonly, nonatomic) NSUInteger currentDownloadCount;//当前下载线程数
@property (assign, nonatomic) NSTimeInterval downloadTimeout;//下载时间限制
@property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder;//下载方式,即FIFO、LIFO。
//下载图片的方法
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url
                                     options:(SDWebImageDownloaderOptions)options
                                    progress:(SDWebImageDownloaderProgressBlock)progressBlock
                                   completed:(SDWebImageDownloaderCompletedBlock)completedBlock;

其中有两个Block,SD对于它们的声明如下

//对于下载进度进行反馈
typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize);
//完成后对图片和数据进行处理,如果出错,则进行出错处理
typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data, NSError *error, BOOL finished);

SDWebImageDownloader中,用到了NSOperationQueue来作为操作队列,因此NSOperationQueue所有操作适用于SDWebImage。

SDWebImageDownloaderOperation

SDWebImageDownloaderOperation中实现NSURLConnectionDataDelegate协议来实现数据的下载,主要通过三个方法

//连接成功
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
//接收数据,.m文件中实现反馈给SDWebImageDownloaderProgressBlock数据长度
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
//下载结束后,将结果反馈给SDWebImageDownloaderCompletedBlock
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection

暂时先写到这边,剩下的源码后续会继续总结

IOS
Web note ad 1