iOS开发中看SDWebImage源码之下载网络图片

整体框架图


0.SD.png

一针见血

SDWebImage使用了NSURLSession下载网络图片。

找网络图片下载代码路线

寻找路线.png
1.UIImageView+WebCache
  - (void)sd_setImageWithURL:(nullable NSURL *)url completed:(nullable SDExternalCompletionBlock)completedBlock {
[self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
}
2.UIView+WebCache
  - (void)sd_internalSetImageWithURL:(nullable NSURL *)url
              placeholderImage:(nullable UIImage *)placeholder
                       options:(SDWebImageOptions)options
                       context:(nullable SDWebImageContext *)context
                 setImageBlock:(nullable SDSetImageBlock)setImageBlock
                      progress:(nullable SDImageLoaderProgressBlock)progressBlock
                     completed:(nullable SDInternalCompletionBlock)completedBlock;

/******************************************************************/

  id <SDWebImageOperation> operation = [manager loadImageWithURL:url options:options context:context progress:combinedProgressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        NSLog(@"image:===%@", image);
        NSLog(@"下载完图片的回调...");
3.SDWebImageManager
   NSLog(@"调用获取图片的方法...");
[self callCacheProcessForOperation:operation url:url options:options context:context progress:progressBlock completed:completedBlock];

/****************************************************************/

   NSLog(@"callDownloadProcessForOperation...");
        [self callDownloadProcessForOperation:strongOperation url:url options:options context:context cachedImage:cachedImage cachedData:cachedData cacheType:cacheType progress:progressBlock completed:completedBlock];

/****************************************************************/

         NSLog(@"调用SDWebImageDownloader下载图片的方法...");
    operation.loaderOperation = [self.imageLoader loadImageWithURL:url options:options context:context progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
4.SDWebImageDownloader
  - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
                                               options:(SDWebImageDownloaderOptions)options
                                               context:(nullable SDWebImageContext *)context
                                              progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                                             completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock {


__weak SDWebImageDownloader *wself = self;
NSLog(@"下载图片...");
return [self addProgressCallback:progressBlock completedBlock:completedBlock forURL:url createCallback:^NSOperation<SDWebImageDownloaderOperation> *{

/****************************************************************/

  NSLog(@"调用SDWebImageDownloaderOperation的addHandlersForProgress方法..");
id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock];
5.SDWebImageDownloaderOperation
  #pragma mark NSURLSessionDataDelegate     
 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
NSLog(@"%s", __func__);
if (!self.imageData) {
    self.imageData = [[NSMutableData alloc] initWithCapacity:self.expectedSize];
}
[self.imageData appendData:data];

在SDWebImageDownloader中创建SDWebImageDownloaderOperation对象的时候将session传递给了SDWebImageDownloaderOperation对象,[self.dataTask resume];的操作是在SDWebImageDownloaderOperation中执行的。

个人疑问:

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

NSLog(@"----%s", __func__);
// Identify the operation that runs this task and pass it the delegate method
NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:dataTask];
if ([dataOperation respondsToSelector:@selector(URLSession:dataTask:didReceiveData:)]) {
    [dataOperation URLSession:session dataTask:dataTask didReceiveData:data];
}
}  

SDWebImageDownloader中的NSURLSessionTaskDelegate代理方法在SDWebImageDownloaderOperation中是如何能执行的?

SDWebImageDownloaderOperation

有两个SDWebImageDownloaderOperation

  1. 遵守了NSURLSessionTaskDelegate, NSURLSessionDataDelegate的协议
  2. 继承自NSOperation的类

本篇文章到这里就结束了,愿大家加班不多工资多,男同胞都有女朋友,女同胞都有男朋友。😊

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容