全文参考链接:zhuanlan.zhihu.com/p/111650375
一.UIView+WebCache 文件
1.NSString *validOperationKey = context[SDWebImageContextSetImageOperationKey];
context为SDWebImageContext类型,查看SDWebImageContext的定义:
typedef NSDictionary<SDWebImageContextOption, id> SDWebImageContext;
可知SDWebImageContext实际上为一个NSDictionary类型,key为SDWebImageContextOption类型,value为id类型。
继续查找SDWebImageContextOption:
typedef NSString * SDWebImageContextOption NS_EXTENSIBLE_STRING_ENUM;
可知SDWebImageContextOption是一个可扩展的String枚举
2.如果查找不到validOperationKey,将validOperationKey设置为类名
validOperationKey = NSStringFromClass([self class]);
3.取消上一次任务,保证没有当前正在进行的异步下载操作, 不会与即将进行的操作发生冲突
[self sd_cancelImageLoadOperationWithKey:validOperationKey];
进入该方法:
查找[self sd_operationDictionary]:
查找SDOperationsDictionary:
typedef NSMapTable<NSString *, **id**<SDWebImageOperation>> SDOperationsDictionary;
可知SDOperationsDictionary为NSMapTable类型,当operation不存在时,通过:
[[NSMapTable alloc] initWithKeyOptions:NSPointerFunctionsStrongMemory valueOptions:NSPointerFunctionsWeakMemory capacity:0];
将SDOperationsDictionary设置为一个strong-weak的NSMapTable
4.if (!(options & SDWebImageDelayPlaceholder))
options类型为SDWebImageOptions:
typedef NS_OPTIONS(NSUInteger, SDWebImageOptions)
通过(options & SDWebImageDelayPlaceholder) 来判断该options的值是否为SDWebImageDelayPlaceholder(这个flag开启会延迟占位图显示的时间,等到图片下载完成之后才会显示占位图.),如果不是,则添加临时占位图
dispatch_main_async_safe(^{ [self sd_setImage:placeholder imageData:nil basedOnClassOrViaCustomSetImageBlock:setImageBlock cacheType:SDImageCacheTypeNone imageURL:url]; });
查找dispatch_main_async_safe:
如果当前队列为主队列则直接执行,否则回到主队列再执行
进入loadImageWithURL方法