【iOS笔记】【14】SDWebImage浅析

本文最后更新于:2021年12月22日 中午

【主页】系列文章目录

【iOS笔记】系列目录


一、官方图片

二、流程梳理

1、需要获取图片的类

调用方法去请求图片资源(APNG,Gif,WebP);

1
2
3
- (void)sd_setImageWithURL:(nullable NSURL *)url 
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options;

2、UIWebImage+WebCache

调用方法去请求图片资源

1
2
3
4
5
6
7
- (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为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
validOperationKey = NSStringFromClass([self class])
````

此时会获取该key的operation调用`[operation cancel]`;

<h3 id='2-3-2'>(2)、获取SDWebImageManager单例</h3>

从上下文获取或者直接获取`[SDWebImageManager sharedManager]`单例对象;

<h3 id='2-3-3'> (3)、取消和存储operation</h3>

获取`SDWebImageManager`单例对象,调用[4、SDWebImageManager 中的(1)](#2-4-1)中的方法获取operation,调用`[self sd_setImageLoadOperation:operation forKey:validOperationKey];`先执行`[operation cancel];`再将operation存放在字典中;

<h2 id='2-4'>4、SDWebImageManager</h2>

<h3 id='2-4-1'> (1)、获取operation</h3>

调用`loadImageWithURL:options:progress:completed:`请求图片资源,该方法返回一个遵循`SDWebImageOperation`协议的`SDWebImageCombinedOperation`对象operation;

<h3 id='2-4-2'> (2)、通过url查询key</h3>

(**PS:和[3UIView+WebCache (1)、该类管理一个字典](#2-3-1)中获取的key不同**)

```objc
NSString *key = [self cacheKeyForURL:url context:context];

(3)、获取SDImageCachesManager

从上下文获取或直接获取[SDImageCachesManager sharedManager]单例对象,SDImageCachesManager遵循SDImageCache协议;

5、SDImageCachesManager

调用方法

1
2
3
4
5
- (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中的方法去存储的缓存中获取(我理解的是磁盘中),获取到以后,调用下面这个方法进行存储

1
2
// 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,走下面下载逻辑

1
2
3
4
5
- (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,走存储和刷新逻辑

1
2
// 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、需要获取图片的类

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没有设置默认值,所以在默认情况下不会对缓存空间设置限制。

四、官方其他图片


联系方式

邮箱: xiebangyao_1994@163.com

相关账号: