SDWebImage源码解读SDWebImageDownloader注意

图片下载的这些回调信息存储在SDWebImageDownloader类的URLOperations属性中,该属性是一个字典,key是图片的URL地址,value则是一个SDWebImageDownloaderOperation对象,包含每个图片的多组回调信息。由于我们允许多个图片同时下载,因此可能会有多个线程同时操作URLOperations属性。为了保证URLOperations操作(添加、删除)的线程安全性,SDWebImageDownloader将这些操作作为一个个任务放到barrierQueue队列中,并设置屏障来确保同一时间只有一个线程操作URLOperations属性,我们以添加操作为例,如下代码所示:

- (nullableSDWebImageDownloadToken *)addProgressCallback:(SDWebImageDownloaderProgressBlock)progressBlock

completedBlock:(SDWebImageDownloaderCompletedBlock)completedBlock

forURL:(nullableNSURL*)url

createCallback:(SDWebImageDownloaderOperation *(^)())createCallback {

...

// 1. 以dispatch_barrier_sync操作来保证同一时间只有一个线程能对URLOperations进行操作

dispatch_barrier_sync(self.barrierQueue, ^{

SDWebImageDownloaderOperation *operation =self.URLOperations[url];

if(!operation) {

//2. 处理第一次URL的下载

operation = createCallback();

self.URLOperations[url] = operation;

__weakSDWebImageDownloaderOperation *woperation = operation;

operation.completionBlock = ^{

SDWebImageDownloaderOperation *soperation = woperation;

if(!soperation)return;

if(self.URLOperations[url] == soperation) {

[self.URLOperations removeObjectForKey:url];

};

};

}

// 3. 处理同一URL的同步下载请求的单个下载

iddownloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock];

token = [SDWebImageDownloadToken new];

token.url = url;

token.downloadOperationCancelToken = downloadOperationCancelToken;

});

returntoken;

}

整个下载管理器对于下载请求的管理都是放在downloadImageWithURL:options:progress:completed:方法里面来处理的,该方法调用了上面所提到的addProgressCallback:andCompletedBlock:forURL:createCallback:方法来将请求的信息存入管理器中,同时在创建回调的block中创建新的操作,配置之后将其放入downloadQueue操作队列中,最后方法返回新创建的操作。其具体实现如下:

- (nullableSDWebImageDownloadToken *)downloadImageWithURL:(nullableNSURL*)url

options:(SDWebImageDownloaderOptions)options

progress:(nullableSDWebImageDownloaderProgressBlock)progressBlock

completed:(nullableSDWebImageDownloaderCompletedBlock)completedBlock {

__weakSDWebImageDownloader *wself =self;

return[selfaddProgressCallback:progressBlock completedBlock:completedBlock forURL:url createCallback:^SDWebImageDownloaderOperation *{

__strong__typeof(wself) sself = wself;

//超时时间

NSTimeIntervaltimeoutInterval = sself.downloadTimeout;

if(timeoutInterval ==0.0) {

timeoutInterval =15.0;

}

// 1. 创建请求对象,并根据options参数设置其属性

// 为了避免潜在的重复缓存(NSURLCache + SDImageCache),如果没有明确告知需要缓存,则禁用图片请求的缓存操作

NSMutableURLRequest*request = [[NSMutableURLRequestalloc] initWithURL:url cachePolicy:(options & SDWebImageDownloaderUseNSURLCache ?NSURLRequestUseProtocolCachePolicy:NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:timeoutInterval];

request.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies);

request.HTTPShouldUsePipelining =YES;

if(sself.headersFilter) {

request.allHTTPHeaderFields = sself.headersFilter(url, [sself.HTTPHeaderscopy]);

}

else{

request.allHTTPHeaderFields = sself.HTTPHeaders;

}

SDWebImageDownloaderOperation *operation = [[sself.operationClass alloc] initWithRequest:request inSession:sself.session options:options];

operation.shouldDecompressImages = sself.shouldDecompressImages;

if(sself.urlCredential) {

operation.credential = sself.urlCredential;

}elseif(sself.username && sself.password) {

operation.credential = [NSURLCredentialcredentialWithUser:sself.username password:sself.password persistence:NSURLCredentialPersistenceForSession];

}

if(options & SDWebImageDownloaderHighPriority) {

operation.queuePriority =NSOperationQueuePriorityHigh;

}elseif(options & SDWebImageDownloaderLowPriority) {

operation.queuePriority =NSOperationQueuePriorityLow;

}

// 2. 将操作加入到操作队列downloadQueue中

// 如果是LIFO顺序,则将新的操作作为原队列中最后一个操作的依赖,然后将新操作设置为最后一个操作

[sself.downloadQueue addOperation:operation];

if(sself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) {

// Emulate LIFO execution order by systematically adding new operations as last operation's dependency

[sself.lastAddedOperation addDependency:operation];

sself.lastAddedOperation = operation;

}

returnoperation;

}];

}

另外,每个下载操作的超时时间可以通过downloadTimeout属性来设置,默认值为15秒。

下载操作

每个图片的下载操作都是一个Operation操作。。我们在上面分析过这个操作的创建及加入操作队列的过程。现在我们来看看单个操作的具体实现。

SDWebImage定义了一个协议,即SDWebImageOperation作为图片下载操作的基础协议。它只声明了一个cancel方法,用于取消操作。协议的具体声明如下:

@protocolSDWebImageOperation

- (void)cancel;

@end

SDWebImage还定义了一个下载协议,即SDWebImageDownloaderOperationInterface,它允许用户自定义下载操作,当然,SDWebImage也提供了自己的下载类,即SDWebImageDownloaderOperation,它继承自NSOperation,并采用了SDWebImageOperation和SDWebImageDownloaderOperationInterface协议。并且实现他们的代理方法。

对于图片的下载,SDWebImageDownloaderOperation完全依赖于URL加载系统中的NSURLSession类。我们先来分析一下SDWebImageDownloaderOperation类中对于图片实际数据的下载处理,即NSURLSessionDataDelegate和NSURLSessionDataDelegate各个代理方法的实现。(ps 有关NSURLSession类的具体介绍请戳这里)

我们前面说过SDWebImageDownloaderOperation类是继承自NSOperation类。它没有简单的实现main方法,而是采用更加灵活的start方法,以便自己管理下载的状态。

在start方法中,创建了我们下载所使用的NSURLSession对象,开启了图片的下载,同时抛出一个下载开始的通知。当然,如果我们期望下载在后台处理,则只需要配置我们的下载选项,使其包含SDWebImageDownloaderContinueInBackground选项。start方法的具体实现如下:

- (void)start {

@synchronized(self) {

// 管理下载状态,如果已取消,则重置当前下载并设置完成状态为YES

if(self.isCancelled) {

self.finished =YES;

[selfreset];

return;

}

...

NSURLSession*session =self.unownedSession;

if(!self.unownedSession) {

//如果session为空,创建session

NSURLSessionConfiguration*sessionConfig = [NSURLSessionConfigurationdefaultSessionConfiguration];

sessionConfig.timeoutIntervalForRequest =15;

self.ownedSession = [NSURLSessionsessionWithConfiguration:sessionConfig

delegate:self

delegateQueue:nil];

session =self.ownedSession;

}

//创建下载任务

self.dataTask = [session dataTaskWithRequest:self.request];

self.executing =YES;

}

//开启下载任务

[self.dataTask resume];

if(self.dataTask) {

for(SDWebImageDownloaderProgressBlock progressBlockin[selfcallbacksForKey:kProgressCallbackKey]) {

progressBlock(0,NSURLResponseUnknownLength,self.request.URL);

}

// 2. 在主线程抛出下载开始通知

dispatch_async(dispatch_get_main_queue(), ^{

[[NSNotificationCenterdefaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self];

});

}else{

[selfcallCompletionBlocksWithError:[NSErrorerrorWithDomain:NSURLErrorDomaincode:0userInfo:@{NSLocalizedDescriptionKey:@"Connection can't be initialized"}]];

}

...

}

我们先看看NSURLSessionDataDelegate代理的具体实现:

- (void)URLSession:(NSURLSession*)session

dataTask:(NSURLSessionDataTask*)dataTask

didReceiveResponse:(NSURLResponse*)response

completionHandler:(void(^)(NSURLSessionResponseDispositiondisposition))completionHandler {

//接收到服务器响应

if(![response respondsToSelector:@selector(statusCode)] || (((NSHTTPURLResponse*)response).statusCode <400&& ((NSHTTPURLResponse*)response).statusCode !=304)) {

//如果服务器状态码正常,并且不是304,(因为304表示远程图片并没有改变,当前缓存的图片就可以使用)拿到图片的大小。并进度回调

NSIntegerexpected = response.expectedContentLength >0? (NSInteger)response.expectedContentLength :0;

self.expectedSize = expected;

for(SDWebImageDownloaderProgressBlock progressBlockin[selfcallbacksForKey:kProgressCallbackKey]) {

progressBlock(0, expected,self.request.URL);

}

//根据返回数据大小创建一个数据Data容器

self.imageData = [[NSMutableDataalloc] initWithCapacity:expected];

self.response = response;

dispatch_async(dispatch_get_main_queue(), ^{

//发送接收到服务器响应通知

[[NSNotificationCenterdefaultCenter] postNotificationName:SDWebImageDownloadReceiveResponseNotification object:self];

});

}

else{

//状态码错误

NSUIntegercode = ((NSHTTPURLResponse*)response).statusCode;

//判断是不是304

if(code ==304) {

[selfcancelInternal];

}else{

[self.dataTask cancel];

}

dispatch_async(dispatch_get_main_queue(), ^{

//发出停止下载通知

[[NSNotificationCenterdefaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];

});

//错误回调

[selfcallCompletionBlocksWithError:[NSErrorerrorWithDomain:NSURLErrorDomaincode:((NSHTTPURLResponse*)response).statusCode userInfo:nil]];

//重置

[selfdone];

}

if(completionHandler) {

completionHandler(NSURLSessionResponseAllow);

}

}

- (void)URLSession:(NSURLSession*)session dataTask:(NSURLSessionDataTask*)dataTask didReceiveData:(NSData*)data {

//1. 接收服务器返回数据 往容器中追加数据

[self.imageData appendData:data];

if((self.options & SDWebImageDownloaderProgressiveDownload) &&self.expectedSize >0) {

//2. 获取已下载数据总大小

constNSIntegertotalSize =self.imageData.length;

// 3. 更新数据源,我们需要传入所有数据,而不仅仅是新数据

CGImageSourceRefimageSource =CGImageSourceCreateWithData((__bridgeCFDataRef)self.imageData,NULL);

// 4. 首次获取到数据时,从这些数据中获取图片的长、宽、方向属性值

if(width + height ==0) {

CFDictionaryRefproperties =CGImageSourceCopyPropertiesAtIndex(imageSource,0,NULL);

if(properties) {

NSIntegerorientationValue =-1;

CFTypeRefval =CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);

if(val)CFNumberGetValue(val, kCFNumberLongType, &height);

val =CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);

if(val)CFNumberGetValue(val, kCFNumberLongType, &width);

val =CFDictionaryGetValue(properties, kCGImagePropertyOrientation);

if(val)CFNumberGetValue(val, kCFNumberNSIntegerType, &orientationValue);

CFRelease(properties);

// 5. 当绘制到Core Graphics时,我们会丢失方向信息,这意味着有时候由initWithCGIImage创建的图片

//    的方向会不对,所以在这边我们先保存这个信息并在后面使用。

#if SD_UIKIT || SD_WATCH

orientation = [[selfclass] orientationFromPropertyValue:(orientationValue ==-1?1: orientationValue)];

#endif

}

}

// 6. 图片还未下载完成

if(width + height >0&& totalSize

// 7. 使用现有的数据创建图片对象,如果数据中存有多张图片,则取第一张

CGImageRefpartialImageRef =CGImageSourceCreateImageAtIndex(imageSource,0,NULL);

#if SD_UIKIT || SD_WATCH

// 8. 适用于iOS变形图像的解决方案。我的理解是由于iOS只支持RGB颜色空间,所以在此对下载下来的图片做个颜色空间转换处理。

if(partialImageRef) {

constsize_t partialHeight =CGImageGetHeight(partialImageRef);

CGColorSpaceRefcolorSpace =CGColorSpaceCreateDeviceRGB();

CGContextRefbmContext =CGBitmapContextCreate(NULL, width, height,8, width *4, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);

CGColorSpaceRelease(colorSpace);

if(bmContext) {

CGContextDrawImage(bmContext, (CGRect){.origin.x =0.0f, .origin.y =0.0f, .size.width = width, .size.height = partialHeight}, partialImageRef);

CGImageRelease(partialImageRef);

partialImageRef =CGBitmapContextCreateImage(bmContext);

CGContextRelease(bmContext);

}

else{

CGImageRelease(partialImageRef);

partialImageRef =nil;

}

}

#endif

// 9. 对图片进行缩放、解码操作

if(partialImageRef) {

#if SD_UIKIT || SD_WATCH

UIImage*image = [UIImageimageWithCGImage:partialImageRef scale:1orientation:orientation];

#elif SD_MAC

UIImage*image = [[UIImagealloc] initWithCGImage:partialImageRef size:NSZeroSize];

#endif

NSString*key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];

UIImage*scaledImage = [selfscaledImageForKey:key image:image];

if(self.shouldDecompressImages) {

image = [UIImagedecodedImageWithImage:scaledImage];

}

else{

image = scaledImage;

}

CGImageRelease(partialImageRef);

[selfcallCompletionBlocksWithImage:image imageData:nilerror:nilfinished:NO];

}

}

CFRelease(imageSource);

}

for(SDWebImageDownloaderProgressBlock progressBlockin[selfcallbacksForKey:kProgressCallbackKey]) {

progressBlock(self.imageData.length,self.expectedSize,self.request.URL);

}

}

当然,在下载完成或下载失败后,会调用NSURLSessionTaskDelegate的- (void)URLSession: task: didCompleteWithError:代理方法,并清除连接,并抛出下载停止的通知。如果下载成功,则会处理完整的图片数据,对其进行适当的缩放与解压缩操作,以提供给完成回调使用。

小结

下载的核心其实就是利用NSURLSession对象来加载数据。每个图片的下载都由一个Operation操作来完成,并将这些操作放到一个操作队列中。这样可以实现图片的并发下载。

缓存

为了减少网络流量的消耗,我们都希望下载下来的图片缓存到本地,下次再去获取同一张图片时,可以直接从本地获取,而不再从远程服务器获取。这样做的一个好处是提升了用户体验,用户第二次查看同一幅图片时,能快速从本地获取图片直接呈现给用户。

SDWebImage提供了对图片缓存的支持,而该功能是由SDImageCache类完成的。该类负责处理内存缓存及一个可选的磁盘缓存。其中磁盘缓存的写操作是异步的,这样就不会对UI操作造成影响。

配置

另外说明,在4.0以后新添加一个缓存配置类SDImageCacheConfig,主要是一些缓存策略的配置。其头文件定义如下:

/**

是否在缓存的时候解压缩,默认是YES 可以提高性能 但是会耗内存。 当使用SDWebImage 因为内存而崩溃 可以将其设置为NO

*/

@property(assign,nonatomic)BOOLshouldDecompressImages;

/**

* 是否禁用 iCloud 备份 默认YES

*/

@property(assign,nonatomic)BOOLshouldDisableiCloud;

/**

* 内存缓存  默认YES

*/

@property(assign,nonatomic)BOOLshouldCacheImagesInMemory;

/**

* 最大磁盘缓存时间 默认一周 单位秒

*/

@property(assign,nonatomic)NSIntegermaxCacheAge;

/**

* 最大缓存容量 0 表示无限缓存  单位字节

*/

@property(assign,nonatomic)NSUIntegermaxCacheSize;

内存缓存及磁盘缓存

内存缓存的处理使用NSCache对象来实现的。NSCache是一个类似与集合的容器。它存储key-value对,这一点类似于NSDictionary类。我们通常使用缓存来临时存储短时间使用但创建昂贵的对象。重用这些对象可以优化性能,因为它们的值不需要重新计算。另外一方面,这些对象对于程序员来说不是紧要的,在内存紧张时会被丢弃。

磁盘缓存的处理则是使用NSFileManager对象来实现的。图片存储的位置是位于Caches文件夹中的default文件夹下。另外,SDImageCache还定义了一个串行队列,来异步存储图片。

内存缓存与磁盘缓存相关变量的声明及定义如下:

@interfaceSDImageCache()

#pragma mark - Properties

@property(strong,nonatomic,nonnull)NSCache*memCache;

@property(strong,nonatomic,nonnull)NSString*diskCachePath;

@property(strong,nonatomic,nullable)NSMutableArray *customPaths;

@property(SDDispatchQueueSetterSementics,nonatomic,nullable)dispatch_queue_tioQueue;

@end

@implementationSDImageCache{

NSFileManager*_fileManager;

}

- (nonnullinstancetype)initWithNamespace:(nonnullNSString*)ns

diskCacheDirectory:(nonnullNSString*)directory {

if((self= [superinit])) {

NSString*fullNamespace = [@"com.hackemist.SDWebImageCache."stringByAppendingString:ns];

// 队列

_ioQueue = dispatch_queue_create("com.hackemist.SDWebImageCache", DISPATCH_QUEUE_SERIAL);

//缓存配置

_config = [[SDImageCacheConfig alloc] init];

// 内存缓存

_memCache = [[AutoPurgeCache alloc] init];

_memCache.name = fullNamespace;

// 初始化磁盘缓存路径

if(directory !=nil) {

_diskCachePath = [directory stringByAppendingPathComponent:fullNamespace];

}else{

NSString*path = [selfmakeDiskCachePath:ns];

_diskCachePath = path;

}

dispatch_sync(_ioQueue, ^{

_fileManager = [NSFileManagernew];

});

}

returnself;

}

@end

SDImageCache提供了大量方法来缓存、获取、移除、及清空图片。而对于每一个图片,为了方便地在内存或磁盘中对它进行这些操作,我们需要一个key值来索引它。在内存中,我们将其作为NSCache的key值,而在磁盘中,我们用作这个key作为图片的文件名。对于一个远程服务器下载的图片,其url实作为这个key的最佳选择了。我们在后面会看到这个key值得重要性。

存储图片

我们先来看看图片的缓存操作,该操作会在内存中放置一份缓存,而如果确定需要缓存到磁盘,则将磁盘缓存操作作为一个task放到串行队列中处理。在iOS中,会先检测图片是PNG还是JPEG,并将其转换为相应的图片数据,最后将数据写入到磁盘中(文件名是对key值做MD5摘要后的串)。缓存操作的基础方法是:-storeImage:imageData:forKey:toDisk:completion:,它的具体实现如下:

- (void)storeImage:(nullableUIImage*)image

imageData:(nullableNSData*)imageData

forKey:(nullableNSString*)key

toDisk:(BOOL)toDisk

completion:(nullableSDWebImageNoParamsBlock)completionBlock {

if(!image || !key) {

if(completionBlock) {

completionBlock();

}

return;

}

// 内存缓存 将其存入NSCache中,同时传入图片的消耗值

if(self.config.shouldCacheImagesInMemory) {

NSUIntegercost = SDCacheCostForImage(image);

[self.memCache setObject:image forKey:key cost:cost];

}

// 如果确定需要磁盘缓存,则将缓存操作作为一个任务放入ioQueue中

if(toDisk) {

dispatch_async(self.ioQueue, ^{

NSData*data = imageData;

if(!data && image) {

//如果imageData为nil 需要确定图片是PNG还是JPEG。PNG图片容易检测,因为有一个唯一签名。PNG图像的前8个字节总是包含以下值:137 80 78 71 13 10 26 10

//判断 图片是何种类型 使用 sd_imageFormatForImageData 来判断

// SDImageFormat 是一个枚举  其定义如下:

//                typedef NS_ENUM(NSInteger, SDImageFormat) {

//                    SDImageFormatUndefined = -1,

//                    SDImageFormatJPEG = 0,

//                    SDImageFormatPNG,

//                    SDImageFormatGIF,

//                    SDImageFormatTIFF,

//                    SDImageFormatWebP

//                };

SDImageFormat imageFormatFromData = [NSDatasd_imageFormatForImageData:data];

//根据图片类型 转成data

data = [image sd_imageDataAsFormat:imageFormatFromData];

}

// 4. 创建缓存文件并存储图片

[selfstoreImageDataToDisk:data forKey:key];

if(completionBlock) {

dispatch_async(dispatch_get_main_queue(), ^{

completionBlock();

});

}

});

}else{

if(completionBlock) {

completionBlock();

}

}

}

查询图片

如果我们想在内存或磁盘中查询是否有key指定的图片,则可以分别使用以下方法:

//快速查询图片是否已经磁盘缓存 不返回图片 只做快速查询 异步操作

- (void)diskImageExistsWithKey:(nullableNSString*)key completion:(nullableSDWebImageCheckCacheCompletionBlock)completionBlock;

//异步查询图片 不管是内存缓存还是磁盘缓存

- (nullableNSOperation*)queryCacheOperationForKey:(nullableNSString*)key done:(nullableSDCacheQueryCompletedBlock)doneBlock;

//从内存中查询图片

- (nullableUIImage*)imageFromMemoryCacheForKey:(nullableNSString*)key;

//从磁盘中查询图片

- (nullableUIImage*)imageFromDiskCacheForKey:(nullableNSString*)key;

//同步查询图片,不管是内存缓存还是磁盘缓存

- (nullableUIImage*)imageFromCacheForKey:(nullableNSString*)key;

其实- (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key内部实现是调用了- (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key和- (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key方法,如下:

- (nullableUIImage*)imageFromCacheForKey:(nullableNSString*)key {

// 从缓存中查找图片

UIImage*image = [selfimageFromMemoryCacheForKey:key];

if(image) {

returnimage;

}

// 从磁盘中查找图片

image = [selfimageFromDiskCacheForKey:key];

returnimage;

}

我们再来看看异步查询图片的具体实现:

- (nullableNSOperation*)queryCacheOperationForKey:(nullableNSString*)key done:(nullableSDCacheQueryCompletedBlock)doneBlock {

...

// 1. 首先查看内存缓存,如果查找到,则直接回调doneBlock并返回

UIImage*image = [selfimageFromMemoryCacheForKey:key];

if(image) {

NSData*diskData =nil;

//进行了是否是GIF的判断

if([image isGIF]) {

diskData = [selfdiskImageDataBySearchingAllPathsForKey:key];

}

if(doneBlock) {

doneBlock(image, diskData, SDImageCacheTypeMemory);

}

returnnil;

}

// 2. 如果内存中没有,则在磁盘中查找。如果找到,则将其放到内存缓存,并调用doneBlock回调

NSOperation*operation = [NSOperationnew];

dispatch_async(self.ioQueue, ^{

if(operation.isCancelled) {

// do not call the completion if cancelled

return;

}

@autoreleasepool{

NSData*diskData = [selfdiskImageDataBySearchingAllPathsForKey:key];

UIImage*diskImage = [selfdiskImageForKey:key];

if(diskImage &&self.config.shouldCacheImagesInMemory) {

//进行内存缓存

NSUIntegercost = SDCacheCostForImage(diskImage);

[self.memCache setObject:diskImage forKey:key cost:cost];

}

if(doneBlock) {

dispatch_async(dispatch_get_main_queue(), ^{

doneBlock(diskImage, diskData, SDImageCacheTypeDisk);

});

}

}

});

returnoperation;

}

移除图片

图片的移除操作则可以使用以下方法:

//从内存和磁盘中移除图片

- (void)removeImageForKey:(nullableNSString*)key withCompletion:(nullableSDWebImageNoParamsBlock)completion;

//从内存 或 可选磁盘中移除图片

- (void)removeImageForKey:(nullableNSString*)key fromDisk:(BOOL)fromDisk withCompletion:(nullableSDWebImageNoParamsBlock)completion;

我们可以选择同时移除内存及磁盘上的图片,或者只移除内存中的图片。

清理图片

磁盘缓存图片的操作可以分为完全清空和部分清理。完全清空操作是直接把缓存的文件夹移除,部分清理是清理掉过时的旧图片,清空操作有以下方法:

//清除内存缓存

- (void)clearMemory;

//完全清空磁盘缓存

- (void)clearDiskOnCompletion:(nullableSDWebImageNoParamsBlock)completion;

//清空旧图片

- (void)deleteOldFilesWithCompletionBlock:(nullableSDWebImageNoParamsBlock)completionBlock;

而部分清理则是根据我们设定的一些参数来移除一些文件,这里主要有两个指标:文件的缓存有效期及最大缓存空间大小。文件的缓存有效期可以通过SDImageCacheConfig类的maxCacheAge属性来设置,默认是1周的时间。如果文件的缓存时间超过这个时间值,则将其移除。而最大缓存空间大小是通过maxCacheSize属性来设置的,如果所有缓存文件的总大小超过这一大小,则会按照文件最后修改时间的逆序,以每次一半的递归来移除那些过早的文件,直到缓存的实际大小小于我们设置的最大使用空间。清理的操作在-deleteOldFilesWithCompletionBlock:方法中,其实现如下:

- (void)deleteOldFilesWithCompletionBlock:(nullableSDWebImageNoParamsBlock)completionBlock {

dispatch_async(self.ioQueue, ^{

NSURL*diskCacheURL = [NSURLfileURLWithPath:self.diskCachePath isDirectory:YES];

NSArray *resourceKeys = @[NSURLIsDirectoryKey,NSURLContentModificationDateKey,NSURLTotalFileAllocatedSizeKey];

// 1. 该枚举器预先获取缓存文件的有用的属性

NSDirectoryEnumerator*fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL

includingPropertiesForKeys:resourceKeys

options:NSDirectoryEnumerationSkipsHiddenFiles

errorHandler:NULL];

NSDate*expirationDate = [NSDatedateWithTimeIntervalSinceNow:-self.config.maxCacheAge];

NSMutableDictionary *> *cacheFiles = [NSMutableDictionarydictionary];

NSUIntegercurrentCacheSize =0;

// 2. 枚举缓存文件夹中所有文件,该迭代有两个目的:移除比过期日期更老的文件;存储文件属性以备后面执行基于缓存大小的清理操作

NSMutableArray *urlsToDelete = [[NSMutableArrayalloc] init];

for(NSURL*fileURLinfileEnumerator) {

NSError*error;

NSDictionary *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:&error];

// 3. 跳过文件夹

if(error || !resourceValues || [resourceValues[NSURLIsDirectoryKey] boolValue]) {

continue;

}

// 4. 移除早于有效期的老文件

NSDate*modificationDate = resourceValues[NSURLContentModificationDateKey];

if([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {

[urlsToDelete addObject:fileURL];

continue;

}

// 5. 存储文件的引用并计算所有文件的总大小,以备后用

NSNumber*totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];

currentCacheSize += totalAllocatedSize.unsignedIntegerValue;

cacheFiles[fileURL] = resourceValues;

}

for(NSURL*fileURLinurlsToDelete) {

[_fileManager removeItemAtURL:fileURL error:nil];

}

//6.如果磁盘缓存的大小大于我们配置的最大大小,则执行基于文件大小的清理,我们首先删除最老的文件

if(self.config.maxCacheSize >0&& currentCacheSize >self.config.maxCacheSize) {

// 7. 以设置的最大缓存大小的一半作为清理目标

constNSUIntegerdesiredCacheSize =self.config.maxCacheSize /2;

// 8. 按照最后修改时间来排序剩下的缓存文件

NSArray *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent

usingComparator:^NSComparisonResult(idobj1,idobj2) {

return[obj1[NSURLContentModificationDateKey] compare:obj2[NSURLContentModificationDateKey]];

}];

// 9. 删除文件,直到缓存总大小降到我们期望的大小

for(NSURL*fileURLinsortedFiles) {

if([_fileManager removeItemAtURL:fileURL error:nil]) {

NSDictionary *resourceValues = cacheFiles[fileURL];

NSNumber*totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];

currentCacheSize -= totalAllocatedSize.unsignedIntegerValue;

if(currentCacheSize < desiredCacheSize) {

break;

}

}

}

}

if(completionBlock) {

dispatch_async(dispatch_get_main_queue(), ^{

completionBlock();

});

}

});

}

小结

以上分析了图片缓存操作,当然,除了上面讲的几个操作,SDWebImage类还提供了一些辅助方法。如获取缓存大小、缓存中图片的数量、判断缓存中是否存在某个key指定的图片。另外,SDWebImage类提供了一个单例方法的实现,所以我们可以将其当做单例对象来处理。

SDWebImageManager

在实际的运用中,我们并不直接使用SDWebImageDownloader类及SDImageCache类来执行图片的下载及缓存。为了方便用户的使用,SDWebImage提供了SDWebImageManager对象来管理图片的下载与缓存。而我们经常用到的诸如UIImageView+WebCache等控件的分类都是基于SDWebImageManager对象的,该对象将一个下载器和一个图片缓存绑定在一起,并对外提供两个只读属性来获取它们,如下代码所示:

@interfaceSDWebImageManager:NSObject

@property(weak,nonatomic)id delegate;

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

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

...

@end

从上面的代码中我们还可以看到一个delegate属性,它是一个id 对象。SDWebImageManagerDelegate声明了两个可选实现的方法,如下所示:

// 控制当图片在缓存中没有找到时,应该下载哪个图片

- (BOOL)imageManager:(SDWebImageManager *)imageManager shouldDownloadImageForURL:(NSURL*)imageURL;

// 允许在图片已经被下载完成且被缓存到磁盘或内存前立即转换

- (UIImage*)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage*)image withURL:(NSURL*)imageURL;

这两个代理方法会在SDWebImageManager的-downloadImageWithURL:options:progress:completed:方法中调用,而这个方法是SDWebImageManager类的核心所在。我们来看看它具体的实现:

- (id)loadImageWithURL:(nullableNSURL*)url

options:(SDWebImageOptions)options

progress:(nullableSDWebImageDownloaderProgressBlock)progressBlock

completed:(nullableSDInternalCompletionBlock)completedBlock {

...

// 前面省略n行。主要作了如下处理:

// 1. 判断url的合法性

// 2. 创建SDWebImageCombinedOperation对象

// 3. 查看url是否是之前下载失败过的

// 4. 如果url为nil,或者在不可重试的情况下是一个下载失败过的url,则直接返回操作对象并调用完成回调

operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key done:^(UIImage*cachedImage,NSData*cachedData, SDImageCacheType cacheType) {

if(operation.isCancelled) {

[selfsafelyRemoveOperationFromRunning:operation];

return;

}

//先去缓存中查找图片,如果图片不存在  或者 当前图片的下载模式是 SDWebImageRefreshCached 开始下载

if((!cachedImage || options & SDWebImageRefreshCached) && (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:selfshouldDownloadImageForURL:url])) {

...

//下载

SDWebImageDownloadToken *subOperationToken = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage*downloadedImage,NSData*downloadedData,NSError*error,BOOLfinished) {

__strong__typeof(weakOperation) strongOperation = weakOperation;

if(!strongOperation || strongOperation.isCancelled) {

// 操作被取消,则不做任务事情

}elseif(error) {

// 如果出错,则调用完成回调,并将url放入下载失败url数组中

...

}

else{

...

BOOLcacheOnDisk = !(options & SDWebImageCacheMemoryOnly);

if(options & SDWebImageRefreshCached && cachedImage && !downloadedImage) {

// Image refresh hit the NSURLCache cache, do not call the completion block

}elseif(downloadedImage && (!downloadedImage.images || (options & SDWebImageTransformAnimatedImage)) && [self.delegate respondsToSelector:@selector(imageManager:transformDownloadedImage:withURL:)]) {

// 在全局队列中并行处理图片的缓存

// 首先对图片做个转换操作,该操作是代理对象实现的

// 然后对图片做缓存处理

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{

UIImage*transformedImage = [self.delegate imageManager:selftransformDownloadedImage:downloadedImage withURL:url];

if(transformedImage && finished) {

BOOLimageWasTransformed = ![transformedImage isEqual:downloadedImage];

// pass nil if the image was transformed, so we can recalculate the data from the image

[self.imageCache storeImage:transformedImage imageData:(imageWasTransformed ?nil: downloadedData) forKey:key toDisk:cacheOnDisk completion:nil];

}

...

});

}else{

if(downloadedImage && finished) {

[self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key toDisk:cacheOnDisk completion:nil];

}

...

}

}

// 下载完成并缓存后,将操作从队列中移除

if(finished) {

[selfsafelyRemoveOperationFromRunning:strongOperation];

}

}];

operation.cancelBlock = ^{

[self.imageDownloader cancel:subOperationToken];

__strong__typeof(weakOperation) strongOperation = weakOperation;

[selfsafelyRemoveOperationFromRunning:strongOperation];

};

}elseif(cachedImage) {

...

}else{

...

}

}];

returnoperation;

}

对于这个方法,我们没有做过多的解释。其主要就是下载图片并根据操作选项缓存图片。上面这个下载方法的操作选项参数是由枚举SDWebImageOptions来定义的,这个操作中的一些选项是与SDWebImageDownloaderOptions中的选项对应的,我们来看看这个SDWebImageOptions选项都有哪些:

typedefNS_OPTIONS(NSUInteger, SDWebImageOptions) {

// 默认情况下,当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,

// 在iOS 4+系统中,当程序进入后台后继续下载图片。这将要求系统给予额外的时间让请求完成

// 如果后台任务超时,则操作被取消

SDWebImageContinueInBackground =1<<5,

// 通过设置NSMutableURLRequest.HTTPShouldHandleCookies = YES;来处理存储在NSHTTPCookieStore中的cookie

SDWebImageHandleCookies =1<<6,

// 允许不受信任的SSL认证

SDWebImageAllowInvalidSSLCertificates =1<<7,

// 默认情况下,图片下载按入队的顺序来执行。该标记将其移到队列的前面,

// 以便图片能立即下载而不是等到当前队列被加载

SDWebImageHighPriority =1<<8,

// 默认情况下,占位图片在加载图片的同时被加载。该标记延迟占位图片的加载直到图片已以被加载完成

SDWebImageDelayPlaceholder =1<<9,

// 通常我们不调用动画图片的transformDownloadedImage代理方法,因为大多数转换代码可以管理它。

// 使用这个票房则不任何情况下都进行转换。

SDWebImageTransformAnimatedImage =1<<10,

};

大家再看-downloadImageWithURL:options:progress:completed:,可以看到两个SDWebImageOptions与SDWebImageDownloaderOptions中的选项是如何对应起来的,在此不多做解释。

视图扩展

我们在使用SDWebImage的时候,使用最多的是UIImageView+WebCache中的针对UIImageView的扩展方法,这些扩展方法将UIImageView与WebCache集成在一起,来让UIImageView对象拥有异步下载和缓存远程图片的能力。在4.0.0版本以后,给UIView新增了好多方法,其中最之前UIImageView+WebCache最核心的方法-sd_setImageWithURL:placeholderImage:options:progress:completed:,现在使用的是UIView+WebCache中新增的方法sd_internalSetImageWithURL:placeholderImage:options:operationKey:setImageBlock:progress:completed:,其使用SDWebImageManager单例对象下载并缓存图片,完成后将图片赋值给UIImageView对象的image属性,以使图片显示出来,其具体实现如下:

- (void)sd_internalSetImageWithURL:(nullableNSURL*)url

placeholderImage:(nullableUIImage*)placeholder

options:(SDWebImageOptions)options

operationKey:(nullableNSString*)operationKey

setImageBlock:(nullableSDSetImageBlock)setImageBlock

progress:(nullableSDWebImageDownloaderProgressBlock)progressBlock

completed:(nullableSDExternalCompletionBlock)completedBlock {

...

if(url) {

// check if activityView is enabled or not

if([selfsd_showActivityIndicatorView]) {

[selfsd_addActivityIndicator];

}

__weak__typeof(self)wself =self;

// 使用SDWebImageManager单例对象来下载图片

id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage*image,NSData*data,NSError*error, SDImageCacheType cacheType,BOOLfinished,NSURL*imageURL) {

__strong__typeof(wself) sself = wself;

[sself sd_removeActivityIndicator];

if(!sself) {

return;

}

dispatch_main_async_safe(^{

if(!sself) {

return;

}

if(image && (options & SDWebImageAvoidAutoSetImage) && completedBlock) {

completedBlock(image, error, cacheType, url);

return;

}elseif(image) {

// 图片下载完后显示图片

[sself sd_setImage:image imageData:data basedOnClassOrViaCustomSetImageBlock:setImageBlock];

[sself sd_setNeedsLayout];

}else{

if((options & SDWebImageDelayPlaceholder)) {

[sself sd_setImage:placeholder imageData:nilbasedOnClassOrViaCustomSetImageBlock:setImageBlock];

[sself sd_setNeedsLayout];

}

}

if(completedBlock && finished) {

completedBlock(image, error, cacheType, url);

}

});

}];

[selfsd_setImageLoadOperation:operation forKey:validOperationKey];

}else{

...

}

}

除了扩展UIImageView之外,SDWebImage还扩展了UIView、UIButton、MKAnnotationView等视图类,大家可以参考源码。

当然,如果不想使用这些扩展,则可以直接使用SDWebImageManager来下载图片,这也是很OK的。

技术点

SDWebImage的主要任务就是图片的下载和缓存。为了支持这些操作,它主要使用了以下知识点:

dispatch_barrier_sync函数:该方法用于对操作设置等待,确保在执行完任务后才会执行后续操作。该方法常用于确保类的线程安全性操作。

NSMutableURLRequest:用于创建一个网络请求对象,我们可以根据需要来配置请求报头等信息。

NSOperation及NSOperationQueue:操作队列是Objective-C中一种高级的并发处理方法,现在它是基于GCD来实现的。相对于GCD来说,操作队列的优点是可以取消在任务处理队列中的任务,另外在管理操作间的依赖关系方面也容易一些。对SDWebImage中我们就看到了如何使用依赖将下载顺序设置成后进先出的顺序。(有兴趣的同学可以看看我这篇博客->聊一聊NSOperation的那些事)

NSURLSession:用于网络请求及响应处理。在iOS7.0后,苹果推出了一套新的网络请求接口,即NSURLSession类。(有兴趣的同学可以看看我这篇博客->NSURLSession与NSURLConnection区别)

开启一个后台任务。

NSCache类:一个类似于集合的容器。它存储key-value对,这一点类似于NSDictionary类。我们通常用使用缓存来临时存储短时间使用但创建昂贵的对象。重用这些对象可以优化性能,因为它们的值不需要重新计算。另外一方面,这些对象对于程序来说不是紧要的,在内存紧张时会被丢弃。

清理缓存图片的策略:特别是最大缓存空间大小的设置。如果所有缓存文件的总大小超过这一大小,则会按照文件最后修改时间的逆序,以每次一半的递归来移除那些过早的文件,直到缓存的实际大小小于我们设置的最大使用空间。

对图片的解压缩操作:这一操作可以查看SDWebImageDecoder.m中+decodedImageWithImage方法的实现。

对GIF图片的处理

对WebP图片的处理

对cell的重用机制的解决,利用runtime的关联对象,会为imageView对象关联一个下载列表,当tableView滑动时,imageView重设数据源(url)时,会cancel掉下载列表中所有的任务,然后开启一个新的下载任务。这样子就保证了只有当前可见的cell对象的imageView对象关联的下载任务能够回调,不会发生image错乱。

感兴趣的同学可以深入研究一下这些知识点。当然,这只是其中一部分,更多的知识还有待大家去发掘。

标签

源码分析

上一篇

下一篇

网友跟贴

0人参与

留下你的👣吧^_^

快速登录:

发表跟贴

最新

最热

网易云跟贴,有你更精彩

Copyrights © 2017 贵永冬. All Rights Reserved.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容