美文网首页
iOS开发中看SDWebImage源码之下载网络图片

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

作者: 梁森的简书 | 来源:发表于2018-11-28 16:31 被阅读33次

    一针见血

    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中是如何能执行的?

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

    相关文章

      网友评论

          本文标题:iOS开发中看SDWebImage源码之下载网络图片

          本文链接:https://www.haomeiwen.com/subject/cpczqqtx.html