iOS OCiOS开发

SDWebImage源码解读(二)

2018-07-02  本文已影响13人  智小融

今天总计一下SDWebImage核心模块之下载模块。下载模块主要包括两个类SDWebImageDownloader和SDWebImageDownloaderOperation。其中SDWebImageDownloader负责对所有下载任务的管理,SDWebImageDownloaderOperation负责具体的一个下载任务的执行。另外为了拓展下载功能,还支持实现SDWebImageDownloaderOperationInterface协议来自定义SDWebImageDownloaderOperation,根据需求自定义下载行为。

SDWebImageDownloader内部构成

SDWebImageDownloader内部持有一个下载队列downloadQueue用于管理所有下载操作,一个缓存字典缓存所有的SDWebImageDownloaderOperation下载操作,一个httpheader的设置字典设置下载的请求头信息,两个信号锁保证缓存字典和header头字典的安全操作。同时持有一个下载的session和下载session的对应的配置文件NSURLSessionConfiguration。

SDWebImageDownloader主要方法

- (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
                                                   options:(SDWebImageDownloaderOptions)options
                                                  progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                                                 completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock {
    __weak SDWebImageDownloader *wself = self;

    return [self addProgressCallback:progressBlock completedBlock:completedBlock forURL:url createCallback:^SDWebImageDownloaderOperation *{
         __strong __typeof (wself) sself = wself;
        NSTimeInterval timeoutInterval = sself.downloadTimeout;
        if (timeoutInterval == 0.0) {
            timeoutInterval = 15.0;
        }

        // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise
      //NSURLRequest生成
        NSURLRequestCachePolicy cachePolicy = options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData;
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url
                                                                    cachePolicy:cachePolicy
                                                                timeoutInterval:timeoutInterval];
        //设置cookie信息
        request.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies);
        request.HTTPShouldUsePipelining = YES;
        //设置header信息
        if (sself.headersFilter) {
            request.allHTTPHeaderFields = sself.headersFilter(url, [sself allHTTPHeaderFields]);
        }
        else {
            request.allHTTPHeaderFields = [sself allHTTPHeaderFields];
        }
        //组建SDWebImageDownloaderOperation
        SDWebImageDownloaderOperation *operation = [[sself.operationClass alloc] initWithRequest:request inSession:sself.session options:options];
        operation.shouldDecompressImages = sself.shouldDecompressImages;
        //设置加密信息
        if (sself.urlCredential) {
            operation.credential = sself.urlCredential;
        } else if (sself.username && sself.password) {
            operation.credential = [NSURLCredential credentialWithUser:sself.username password:sself.password persistence:NSURLCredentialPersistenceForSession];
        }
        //设置优先级
        if (options & SDWebImageDownloaderHighPriority) {
            operation.queuePriority = NSOperationQueuePriorityHigh;
        } else if (options & SDWebImageDownloaderLowPriority) {
            operation.queuePriority = NSOperationQueuePriorityLow;
        }
        //设置队列
        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;
        }

        return operation;
    }];
}

此方法用于对外的暴露,实际上进行了SDWebImageDownloaderOperation的生成的相关设置工作。NSURLRequest生成并设置的header信息、cookie信息设置。NSURLRequest、session、配置options生成DownloaderOperation. DownloaderOperation设置加密、优先级、队列。最后通过addProgressCallback方法将operation缓存,并添加到操作队列中addOperation。

SDWebImageDownloader下载选项

SDWebImageDownloader提供了很多下载选项,可以根据情况进行配置。如设置下载优先级、进度、后台下载,图片缩放等,同时支持先进先出,先进后出的下载方式。

typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
    //下载低优先级
    SDWebImageDownloaderLowPriority = 1 << 0,
    //下载高优先级
    SDWebImageDownloaderHighPriority = 1 << 7,
    // 带有进度
    SDWebImageDownloaderProgressiveDownload = 1 << 1,
    //默认不使用URLCache
    SDWebImageDownloaderUseNSURLCache = 1 << 2,
    //如果图片是在NSURLCAche中读取时,调用completion block时,返回nil,配合SDWebImageDownloaderUseNSURLCache使用
    SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,
    //支持后台下载
    SDWebImageDownloaderContinueInBackground = 1 << 4,
    //支持NSHTTPCookieStore的cookie信息,进而设置NSMutableURLRequest.HTTPShouldHandleCookies=YES
    SDWebImageDownloaderHandleCookies = 1 << 5,
    //允许不信任SSL证书
    SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6,
    //缩放大图片
    SDWebImageDownloaderScaleDownLargeImages = 1 << 8,
};

typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
     //先进先出
    SDWebImageDownloaderFIFOExecutionOrder,
    //先进后出
    SDWebImageDownloaderLIFOExecutionOrder
};

SDWebImageDownloader默认配置

1.解压缩shouldDecompressImages:默认情况下,图片下载下来就解压缩,这是拿空间换时间的做法,让图片在使用时能更快的加载。

2.最大下载数maxConcurrentDownloads:此属性实质是设置下载队列(NSOperationQueue)的最大数并发数。

3.当前最大下载数currentDownloadCount:当前下载队列中未执行完毕的操作个数

4.下载超时时间downloadTimeout:默认设置为15秒

5.下载队列executionOrder:默认是先进先出,先到先下载的队列

SDWebImageDownloaderOperation

SDWebImageDownloaderOperation持有NSURLSessionTask和NSURLRequest,在start方法中创建网络请求并执行,在URLSession:task:didCompleteWithError:中进行数据的整合处理,反馈给外部。

总结:

SDWebImage的下载管理模块SDWebImageDownloader采用了NSOperationQueue来管理队列,而没有使用更加高效的GCD来完成。这是因为下载的业务的复杂性比较高,如优先级设置、取消操作、操作状态的监控、操作的依赖性,这些如果用GCD来封装的话,最后的结果就是写出一个NSOperationQueue出来,所以我猜作者肯定想何必再封装造轮子呢,直接使用NSOperationQueue得了。

上一篇 下一篇

猜你喜欢

热点阅读