YYkit之webImage
阅读源码随意记个笔记吧:
每个imageView在调用方法:
- (void)setImageWithURL:(nullableNSURL*)imageURL
placeholder:(nullableUIImage*)placeholder
options:(YYWebImageOptions)options
progress:(nullableYYWebImageProgressBlock)progress
transform:(nullableYYWebImageTransformBlock)transform
completion:(nullableYYWebImageCompletionBlock)completion;
去获取资源图片时:

首先第一行先判断并确定URL合法性
然后调用全局单例YYWebImageManager,这个类相当于整个图片加载的管家
_YYWebImageSetter是分类UIImageView+YYWebImage用来调用YYWebImageOperation去获取资源的控制类,由于UIImageView+YYWebImage是分类,所以用runtime来模拟一个属性。
int32_tsentinel = [settercancelWithNewURL:imageURL];使用来撤销之前的_operation。
YYWebImageOperation这里是YYWebImageManager和_YYWebImageSetter的交集所在。YYWebImageManager提供配置YYWebImageOperation给_YYWebImageSetter作为属性使用。

这部分代码,前两个判断是和合法性判断。

这一步就是去获取iamge.
第一个判断if(manager.cache&&
!(options &YYWebImageOptionUseNSURLCache) &&
!(options &YYWebImageOptionRefreshImageCache)) {
imageFromMemory = [manager.cachegetImageForKey:[managercacheKeyForURL:imageURL]withType:YYImageCacheTypeMemory];
}
三步,一确定存在cache,二说明使用的不是NSURLCache,三确定没有options没有选择YYWebImageOptionRefreshImageCache,然后从缓存中去获取图片资源。
如果获取到了资源,if (!(options & YYWebImageOptionAvoidSetImage))确保获取到直接赋值image到imageView,就是直接赋值self.image= imageFromMemory;否则在completion中手动处理。
然后调用if(completion) completion(imageFromMemory, imageURL,YYWebImageFromMemoryCacheFast,YYWebImageStageFinished,nil);方法。结束。
否则下一步:
if (!(options & YYWebImageOptionIgnorePlaceHolder)) {
self.image= placeholder;
}如果选择了YYWebImageOptionIgnorePlaceHolder,首先赋值placeholder
然后异步去获取资源:
dispatch_async([_YYWebImageSetter setterQueue],每一个iamgeView持有的_YYWebImageSetter都有一个单例异步队列。
在这个对列中配置- (int32_t)setOperationWithSentinel:(int32_t)sentinel
url:(NSURL*)imageURL
options:(YYWebImageOptions)options
manager:(YYWebImageManager*)manager
progress:(YYWebImageProgressBlock)progress
transform:(YYWebImageTransformBlock)transform
completion:(YYWebImageCompletionBlock)completion方法所需要的参数。


下面部分主要分析这个方法:- (int32_t)setOperationWithSentinel:(int32_t)sentinel
url:(NSURL*)imageURL
options:(YYWebImageOptions)options
manager:(YYWebImageManager*)manager
progress:(YYWebImageProgressBlock)progress
transform:(YYWebImageTransformBlock)transform
completion:(YYWebImageCompletionBlock)completion
首先分析一下_YYWebImageSetter这个类:

为啥说YY代码写的好,就是每一个细节都考虑的这么完美无缺,包括一个getter方法,都要用信号量控制来确保安全。

首先使用OSAtomicIncrement32(&_sentinel);来控制setter和operation调用的是同一个imageURL,_sentinel在这里有点通过引用计数器校验的意思。
下一步是配置YYWebImageOperation,并运行,配置operation是由YYWebImageManager这个类来做:


下面开始分析核心类YYWebImageOperation:

初始化方法配置各种参数。


以上start方法覆盖父类NSOperation的start方法。
加@autoreleasepool是为了减少内存消耗,使用同步锁是线程安全。
条件符合会调用[self performSelector:@selector(_startOperation) onThread:[[self class] _networkThread] withObject:nil waitUntilDone:NO modes:@[NSDefaultRunLoopMode]];方法在network线程中发起资源请求。

这个线程是是YYWebImageOperation单例属性。由NSConnection调用。
下面分析_startOperation方法:


@autoreleasepool中的判断是试图从缓存中获取图片,如果不能通过[self performSelector:@selector(_startRequest:) onThread:[self.class _networkThread] withObject:nil waitUntilDone:NO];方法调用_startRequest去网络获取资源。
下面分析
- (void)_startRequest:(id)object方法:


if ((_options & YYWebImageOptionIgnoreFailedURL) && URLBlackListContains(_request.URL)) 判断imageURL 合法性。
_connection = [[NSURLConnection alloc] initWithRequest:_request delegate:[YYWeakProxy proxyWithTarget:self]];发送请求。
以上就是获取的整个处理流程。下边开始分析从网络请求回资源后的处理:
#pragma mark - NSURLConnectionDelegate runs in operation thread
在NSConnection的回调中分析:





开始部分是正常存储data,并调用_progress(_data.length, _expectedSize);这个block方法。
在调用- (void)connectionDidFinishLoading:(NSURLConnection*)connection方法时,完成回调和缓存:



[self performSelector:@selector(_didReceiveImageFromWeb:) onThread:[self.class _networkThread] withObject:image waitUntilDone:NO];方法中调用缓存和返回image.
在_didReceiveImageFromWeb方法中实现:

