一、官方图片
二、流程梳理
1、需要获取图片的类
调用方法去请求图片资源(APNG,Gif,WebP);
- (void)sd_setImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options;
2、UIWebImage+WebCache
调用方法去请求图片资源
- (void)sd_internalSetImageWithURL:(nullable NSURL *)url
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
context:(nullable SDWebImageContext *)context
setImageBlock:(nullable SDSetImageBlock)setImageBlock
progress:(nullable SDImageLoaderProgressBlock)progressBlock
completed:(nullable SDInternalCompletionBlock)completedBlock;
3、UIView+WebCache
(1)、该类管理一个字典
该字典用来存放遵循SDWebImageOperation协议的键值对,key为
validOperationKey = NSStringFromClass([self class])
此时会获取该key的operation调用[operation cancel];
(2)、获取SDWebImageManager单例
从上下文获取或者直接获取[SDWebImageManager sharedManager]单例对象;
(3)、取消和存储operation
获取SDWebImageManager单例对象,调用4、SDWebImageManager 中的(1)中的方法获取operation,调用[self sd_setImageLoadOperation:operation forKey:validOperationKey];先执行[operation cancel];再将operation存放在字典中;
4、SDWebImageManager
(1)、获取operation
调用loadImageWithURL:options:progress:completed:请求图片资源,该方法返回一个遵循SDWebImageOperation协议的SDWebImageCombinedOperation对象operation;
(2)、通过url查询key
(PS:和3、UIView+WebCache (1)、该类管理一个字典中获取的key不同)
NSString *key = [self cacheKeyForURL:url context:context];
(3)、获取SDImageCachesManager
从上下文获取或直接获取[SDImageCachesManager sharedManager]单例对象,SDImageCachesManager遵循SDImageCache协议;
5、SDImageCachesManager
调用方法
- (id<SDWebImageOperation>)queryImageForKey:(NSString *)key
options:(SDWebImageOptions)options
context:(SDWebImageContext *)context
cacheType:(SDImageCacheType)cacheType
completion:(SDImageCacheQueryCompletionBlock)completionBlock;
根据不同的queryOperationPolicy和cacheType去获取不同的operation和cache,这步是去内存中获取
6、SDWebImageManager
处理上面5、SDImageCachesManager方法调用的回调
(1)、没有获取到内存中的cachedImage,去磁盘获取
调用上面5、SDImageCachesManager中的方法去存储的缓存中获取(我理解的是磁盘中),获取到以后,调用下面这个方法进行存储
// Use the store cache process instead of downloading, and ignore .refreshCached option for now
[self callStoreCacheProcessForOperation:operation url:url options:options context:context downloadedImage:cachedImage downloadedData:cachedData finished:YES progress:progressBlock completed:completedBlock];
(2)、没有获取到磁盘中的cachedImage走下载逻辑
步骤(1)、没有获取到内存中的cachedImage,去磁盘获取也没有获取到cachedImage,走下面下载逻辑
- (nullable id<SDWebImageOperation>)requestImageWithURL:(nullable NSURL *)url
options:(SDWebImageOptions)options
context:(nullable SDWebImageContext *)context
progress:(nullable SDImageLoaderProgressBlock)progressBlock
completed:(nullable SDImageLoaderCompletedBlock)completedBlock;
若没有设置(即使用默认设置),则也会走下面(c)存储逻辑
(3)、获取到了cachedImage,走存储和刷新逻辑
// Continue store cache process
[self callStoreCacheProcessForOperation:operation url:url options:options context:context downloadedImage:downloadedImage downloadedData:downloadedData finished:finished progress:progressBlock completed:completedBlock];
(4)、获取到了image
无论是cachedImage还是获取到了downloadImage,都会走回调返回image给上一级调用者
返回链如下:
SDImageCachesManager ---> 3、UIView+WebCache ---> 2、UIImageView+WebCache ---> 1、需要获取图片的类
[self callCompletionBlockForOperation:operation completion:completedBlock image:cachedImage data:cachedData error:nil cacheType:cacheType finished:YES url:url];
三、注意事项
- 1、SDImageCache 在初始化的时候会注册一些消息通知,在内存警告或退到后台的时候清理内存图片缓存,应用结束的时候清理过期图片;
- 2、从硬盘读取到了图片,会将图片添加到内存缓存中(如果空闲内存过小,会先清空内存缓存)
- 3、图片保存到 SDImageCache 中,内存缓存和硬盘缓存会同时保存;
- 4、不论在哪一步获取到了图片,都会沿着返回链返回给图片调用者用于显示图片;
- 5、二级缓存:内存缓存->磁盘缓存,最后启动下载;
- 6、clearDisk // 清除磁盘缓存上的所有image;
- 7、cleanDisk // 清除磁盘缓存上过期的image;
- 8、默认清除磁盘缓存的时长是7天,先清除掉过期的缓存文件,若空间还不够,按时间先后,清理最早缓存的文件;
- 9、maxCacheSize没有设置默认值,所以在默认情况下不会对缓存空间设置限制。