SDWebImage库是一个支持缓存的异步图片下载库。为了方便使用,它提供了UIImageView, UIButton, MKAnnotationView等UI组件的分类。它的优点包含以下:
- 异步图片下载器
- 异步的内存+磁盘图片缓存,自动缓存过期处理
- 后台图片解压
- 同一个URL不会多次下载的保证机制
- 错误的URL不会多次尝试的保证机制
- 主线程不会被block的保证机制
- 良好的性能
- 使用GCD和ARC
下面以UIImageView为例,讲一下获取图片的流程。
- 首先UIImageView调用sd_setImageWithURL来获得图片。
- 这个接口的内部调用UIView+webCache分类的sd_internalSetImageWithURL。
- sd_internalSetImageWithURL这个方法内部调用SDWebImageManager的loadImageWithURL方法。
- SDWebImageManager是一个总的管理类。它首先通过SDWebImageCache的queryCacheOperationForKey去检查图片的缓存是否存在。
- 如果存在就直接返回图片给最外层。
- 如果不存在就会用SDWebImageDownloader中的downloadImageWithURL方法去下载图片。下载完成后首先通过SDWebImageCache来缓存图片,再返回图片给最外层。
1. SDWebImageManager
- (nullable id <SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url
options:(SDWebImageOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDInternalCompletionBlock)completedBlock;
SDWebImageManager是一个管理类,管理从缓存获取图片和downloader下载图片。
- 它是一个单例。
- 它维护了SDImageCache的单例,用来获取缓存
- 它维护了SDWebImageDownloader的单例,用来下载图片。
- 它维护了一个failedURLs黑名单,如果URL在黑名单中,则会取消操作。
- 它维护了一个runingOperation的数组,可以取消和增加operation。
2. SDImageCache
- (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDCacheQueryCompletedBlock)doneBlock;
SDImageCache是一个缓存管理类,管理NSCache和disk中的缓存。它首先从NSCache中获取缓存,获取不到再从disk中获取。
-
NSCache是一个线程安全的类,在系统内存不足的情况下默认会自动释放部分内存。它的用法和NSMutableDictionary类似。
-
NSCache中找不到,就会另起一个线程去从disk中获取图片。文件名以url的16位md5后的字符串,然后每一位字符转换为16进制数命名,防止文件名冲突。
- (nullable NSString *)cachedFileNameForKey:(nullable NSString *)key { const char *str = key.UTF8String; if (str == NULL) { str = ""; } unsigned char r[CC_MD5_DIGEST_LENGTH]; CC_MD5(str, (CC_LONG)strlen(str), r); NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%@", r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15], [key.pathExtension isEqualToString:@""] ? @"" : [NSString stringWithFormat:@".%@", key.pathExtension]]; return filename; }
-
它可以对所有的缓存整体操作,清理内存缓存和磁盘缓存。而且通过NSNotification注册内存警告,关闭,后台事件,会自动清理过期缓存。事件默认是1周,大小默认没有限制。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearMemory) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteOldFiles) name:UIApplicationWillTerminateNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(backgroundDeleteOldFiles) name:UIApplicationDidEnterBackgroundNotification object:nil];
3. SDWebImageDownloader
- (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
options:(SDWebImageDownloaderOptions)options
progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
- 它是一个单例。
- 它维护了一个downloadQueue(NSOperationQueue),最大并发数为6,默认先进先出。
- (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)sessionConfiguration {
if ((self = [super init])) {
_operationClass = [SDWebImageDownloaderOperation class];
_shouldDecompressImages = YES;
_executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
_downloadQueue = [NSOperationQueue new];
_downloadQueue.maxConcurrentOperationCount = 6;
_downloadQueue.name = @"com.hackemist.SDWebImageDownloader";
_URLOperations = [NSMutableDictionary new];
#ifdef SD_WEBP
_HTTPHeaders = [@{@"Accept": @"image/webp,image/*;q=0.8"} mutableCopy];
#else
_HTTPHeaders = [@{@"Accept": @"image/*;q=0.8"} mutableCopy];
#endif
_barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT);
_downloadTimeout = 15.0;
sessionConfiguration.timeoutIntervalForRequest = _downloadTimeout;
/**
* Create the session for this task
* We send nil as delegate queue so that the session creates a serial operation queue for performing all delegate
* method calls and completion handler calls.
*/
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:nil];
}
return self;
}
-
它维护了一个URLOperations字典,以URL为key,operation为value,从而确保同一个URL不会多次下载。同时通过dispatch_barrier_sync来同步执行下载操作。
- (nullable SDWebImageDownloadToken *)addProgressCallback:(SDWebImageDownloaderProgressBlock)progressBlock completedBlock:(SDWebImageDownloaderCompletedBlock)completedBlock forURL:(nullable NSURL *)url createCallback:(SDWebImageDownloaderOperation *(^)())createCallback { // The URL will be used as the key to the callbacks dictionary so it cannot be nil. If it is nil immediately call the completed block with no image or data. if (url == nil) { if (completedBlock != nil) { completedBlock(nil, nil, nil, NO); } return nil; } __block SDWebImageDownloadToken *token = nil; dispatch_barrier_sync(self.barrierQueue, ^{ SDWebImageDownloaderOperation *operation = self.URLOperations[url]; if (!operation) { operation = createCallback(); self.URLOperations[url] = operation; __weak SDWebImageDownloaderOperation *woperation = operation; operation.completionBlock = ^{ SDWebImageDownloaderOperation *soperation = woperation; if (!soperation) return; if (self.URLOperations[url] == soperation) { [self.URLOperations removeObjectForKey:url]; }; }; } id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock]; token = [SDWebImageDownloadToken new]; token.url = url; token.downloadOperationCancelToken = downloadOperationCancelToken; }); return token; }
网友评论