美文网首页
SDWebImageView源码阅读

SDWebImageView源码阅读

作者: 杰米 | 来源:发表于2016-11-29 00:19 被阅读204次

    SDWebImageDownloaderOperation.h

    NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
    NSString *const SDWebImageDownloadReceiveResponseNotification = @"SDWebImageDownloadReceiveResponseNotification";
    NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
    NSString *const SDWebImageDownloadFinishNotification = @"SDWebImageDownloadFinishNotification";
    发通知时会把self作为参数传出去
    
    static NSString *const kProgressCallbackKey = @"progress";
    static NSString *const kCompletedCallbackKey = @"completed";
    
    // 以上面的两个key取block
    @property (strong, nonatomic, nonnull) NSMutableArray<SDCallbacksDictionary *> *callbackBlocks;
    
    //两种block
    typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL);
    
    typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished);
    
    两种session, unownedSession为nil则用ownedSession
    // This is weak because it is injected by whoever manages this session. If this gets nil-ed out, we won't be able to run
    // the task associated with this operation
    @property (weak, nonatomic, nullable) NSURLSession *unownedSession;
    // This is set if we're using not using an injected NSURLSession. We're responsible of invalidating this one
    @property (strong, nonatomic, nullable) NSURLSession *ownedSession;
    
    //这个queue保证下面的操作都是同步的
    @property (SDDispatchQueueSetterSementics, nonatomic, nullable) dispatch_queue_t barrierQueue;
    
    - (nullable id)addHandlersForProgress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                                completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock {
        SDCallbacksDictionary *callbacks = [NSMutableDictionary new];
        if (progressBlock) callbacks[kProgressCallbackKey] = [progressBlock copy];
        if (completedBlock) callbacks[kCompletedCallbackKey] = [completedBlock copy];
        dispatch_barrier_async(self.barrierQueue, ^{
            [self.callbackBlocks addObject:callbacks];
        });
        return callbacks;
    }
    
    - (nullable NSArray<id> *)callbacksForKey:(NSString *)key {
        __block NSMutableArray<id> *callbacks = nil;
        dispatch_sync(self.barrierQueue, ^{
            // We need to remove [NSNull null] because there might not always be a progress block for each callback
            callbacks = [[self.callbackBlocks valueForKey:key] mutableCopy];
            [callbacks removeObjectIdenticalTo:[NSNull null]];
        });
        return [callbacks copy];    // strip mutability here
    }
    
    - (BOOL)cancel:(nullable id)token {
        __block BOOL shouldCancel = NO;
        dispatch_barrier_sync(self.barrierQueue, ^{
            [self.callbackBlocks removeObjectIdenticalTo:token];
            if (self.callbackBlocks.count == 0) {
                shouldCancel = YES;
            }
        });
        if (shouldCancel) {
            [self cancel];
        }
        return shouldCancel;
    }
    

    session回调下载中图片处理(progress),用于进度和实时更新图片

    - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
        [self.imageData appendData:data];
    
        if ((self.options & SDWebImageDownloaderProgressiveDownload) && self.expectedSize > 0) {
            // The following code is from http://www.cocoaintheshell.com/2011/05/progressive-images-download-imageio/
            // Thanks to the author @Nyx0uf
    
            // Get the total bytes downloaded
            const NSInteger totalSize = self.imageData.length;
    
            // Update the data source, we must pass ALL the data, not just the new bytes
            CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)self.imageData, NULL);
    
            if (width + height == 0) {
                CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
                if (properties) {
                    NSInteger orientationValue = -1;
                    CFTypeRef val = 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);
    
                    // When we draw to Core Graphics, we lose orientation information,
                    // which means the image below born of initWithCGIImage will be
                    // oriented incorrectly sometimes. (Unlike the image born of initWithData
                    // in didCompleteWithError.) So save it here and pass it on later.
    #if SD_UIKIT || SD_WATCH
                    orientation = [[self class] orientationFromPropertyValue:(orientationValue == -1 ? 1 : orientationValue)];
    #endif
                }
            }
    
            if (width + height > 0 && totalSize < self.expectedSize) {
                // Create the image
                CGImageRef partialImageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
    
    #if SD_UIKIT || SD_WATCH
                // Workaround for iOS anamorphic image
                if (partialImageRef) {
                    const size_t partialHeight = CGImageGetHeight(partialImageRef);
                    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
                    CGContextRef bmContext = 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
    
                if (partialImageRef) {
    #if SD_UIKIT || SD_WATCH
                    UIImage *image = [UIImage imageWithCGImage:partialImageRef scale:1 orientation:orientation];
    #elif SD_MAC
                    UIImage *image = [[UIImage alloc] initWithCGImage:partialImageRef size:NSZeroSize];
    #endif
                    NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
                    UIImage *scaledImage = [self scaledImageForKey:key image:image];
                    if (self.shouldDecompressImages) {
                        image = [UIImage decodedImageWithImage:scaledImage];
                    }
                    else {
                        image = scaledImage;
                    }
                    CGImageRelease(partialImageRef);
                    //解码完部分图片通知图片下载回调
                    [self callCompletionBlocksWithImage:image imageData:nil error:nil finished:NO];
                }
            }
    
            CFRelease(imageSource);
        }
    //通知进度回调
        for (SDWebImageDownloaderProgressBlock progressBlock in [self callbacksForKey:kProgressCallbackKey]) {
            progressBlock(self.imageData.length, self.expectedSize, self.request.URL);
        }
    }
    

    核心图片处理(completed),session代理加载结束,成功或者失败

    - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
        @synchronized(self) {
            self.dataTask = nil;
            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
                if (!error) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:self];
                }
            });
        }
        
        if (error) {
            [self callCompletionBlocksWithError:error];
        } else {
            if ([self callbacksForKey:kCompletedCallbackKey].count > 0) {
                /**
                 *  See #1608 and #1623 - apparently, there is a race condition on `NSURLCache` that causes a crash
                 *  Limited the calls to `cachedResponseForRequest:` only for cases where we should ignore the cached response
                 *    and images for which responseFromCached is YES (only the ones that cannot be cached).
                 *  Note: responseFromCached is set to NO inside `willCacheResponse:`. This method doesn't get called for large images or images behind authentication
                 */
                if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached && [[NSURLCache sharedURLCache] cachedResponseForRequest:self.request]) {
                    // hack
                    [self callCompletionBlocksWithImage:nil imageData:nil error:nil finished:YES];
                } else if (self.imageData) {
                    UIImage *image = [UIImage sd_imageWithData:self.imageData];
                    NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
                    image = [self scaledImageForKey:key image:image];
                    
                    // Do not force decoding animated GIFs
                    if (!image.images) {
    //图片是否要变成位图
                        if (self.shouldDecompressImages) {
                            if (self.options & SDWebImageDownloaderScaleDownLargeImages) {
    #if SD_UIKIT || SD_WATCH
                                image = [UIImage decodedAndScaledDownImageWithImage:image];
                                [self.imageData setData:UIImagePNGRepresentation(image)];
    #endif
                            } else {
                                image = [UIImage decodedImageWithImage:image];
                            }
                        }
                    }
                    if (CGSizeEqualToSize(image.size, CGSizeZero)) {
                        [self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}]];
                    } else {
                        [self callCompletionBlocksWithImage:image imageData:self.imageData error:nil finished:YES];
                    }
                } else {
                    [self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Image data is nil"}]];
                }
            }
        }
        [self done];
    }
    

    callbackBlocks里的每个kCompletedCallbackKey对应的回调进行实现。

    //session加载完成后会对所有
    - (void)callCompletionBlocksWithImage:(nullable UIImage *)image
                                imageData:(nullable NSData *)imageData
                                    error:(nullable NSError *)error
                                 finished:(BOOL)finished {
        NSArray<id> *completionBlocks = [self callbacksForKey:kCompletedCallbackKey];
        dispatch_main_async_safe(^{
            for (SDWebImageDownloaderCompletedBlock completedBlock in completionBlocks) {
                completedBlock(image, imageData, error, finished);
            }
        });
    }
    

    一般下载或者从磁盘获取的图片是PNG或者JPG,这是经过编码压缩后的图片数据,不是位图,要把它们渲染到屏幕前就需要进行解码转成位图数据,而这个解码操作比较耗时,iOS默认是在主线程解码,所以SDWebImage将这个过程放到子线程了。
    同时因为位图体积很大,所以磁盘缓存不会直接缓存位图数据,而是编码压缩后的PNG或JPG数据。 来自

    下面是调用取消和完成的对自定义NSOperation

    - (void)cancel {
        @synchronized (self) {
            [self cancelInternal];
        }
    }
    
    - (void)cancelInternal {
        if (self.isFinished) return;
        [super cancel];
    
        if (self.dataTask) {
            [self.dataTask cancel];
            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
            });
    
            // As we cancelled the connection, its callback won't be called and thus won't
            // maintain the isFinished and isExecuting flags.
            if (self.isExecuting) self.executing = NO;
            if (!self.isFinished) self.finished = YES;
        }
    
        [self reset];
    }
    
    - (void)done {
        self.finished = YES;
        self.executing = NO;
        [self reset];
    }
    
    - (void)reset {
        dispatch_barrier_async(self.barrierQueue, ^{
            [self.callbackBlocks removeAllObjects];
        });
        self.dataTask = nil;
        self.imageData = nil;
        if (self.ownedSession) {
            [self.ownedSession invalidateAndCancel];
            self.ownedSession = nil;
        }
    }
    

    SDWebImageDownloader

    @property (strong, nonatomic, nonnull) NSOperationQueue *downloadQueue;
    @property (weak, nonatomic, nullable) NSOperation *lastAddedOperation;
    @property (assign, nonatomic, nullable) Class operationClass;
    //operations
    @property (strong, nonatomic, nonnull) NSMutableDictionary<NSURL *, SDWebImageDownloaderOperation *> *URLOperations;
    @property (strong, nonatomic, nullable) SDHTTPHeadersMutableDictionary *HTTPHeaders;
    // This queue is used to serialize the handling of the network responses of all the download operation in a single queue
    @property (SDDispatchQueueSetterSementics, nonatomic, nullable) dispatch_queue_t barrierQueue;
    
    // The session in which data tasks will run
    @property (strong, nonatomic) NSURLSession *session;
    
    //初始化 session queue headers
    - (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)sessionConfiguration {
        if ((self = [super init])) {
            _operationClass = [SDWebImageDownloaderOperation class];
            _shouldDecompressImages = YES;
    //通过依赖实现FIFO
            _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;
    }
    

    添加下载

    - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
                                                      options:(SDWebImageDownloaderOptions)options
                                                     progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                                                    completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock {
       __weak SDWebImageDownloader *wself = self;
    
    //整个函数返回一个token
       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
           NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 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.HTTPHeaders copy]);
           }
           else {
               request.allHTTPHeaderFields = sself.HTTPHeaders;
           }
    //创建下载的operation
           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;
           }
    
           [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;
           }
    
           return operation;
       }];
    }
    
    //上面方法的返回方法函数
    - (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闭包
               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];
                 };
               };
           }
    //返回的是progress和complete闭包的字典
           id downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock];
    
           token = [SDWebImageDownloadToken new];
           token.url = url;
           token.downloadOperationCancelToken = downloadOperationCancelToken;
       });
    
       return token;
    }
    

    SDWebImageOperation和SDWebImageDownloader类里都实现了NSURLSessionTaskDelegate, NSURLSessionDataDelegate,SDWebImageDownloader作为NSURLSession的代理,实现代理方法,在代理方法里查找队列里对应的operation,实现operation里的NSURLSessionTaskDelegate, NSURLSessionDataDelegate方法处理数据等

    SDWebImageDownloadToken有url(NSURL)和downloadOperationCancelToken(id,NSDictionary,存放SDWebImageDownloaderProgressBlock和SDWebImageDownloaderCompletedBlock)两个参数,SDWebImageDownloadToken由SDWebImageManager调用

    - (id <SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url
                                         options:(SDWebImageOptions)options
                                        progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                                       completed:(nullable SDInternalCompletionBlock)completedBlock 
    

    时生成,当要取消一个下载任务时SDWebImageDownloadToken就派上用场了,

    //SDWebImageDownloader.h
    @property (strong, nonatomic, nonnull) NSMutableDictionary<NSURL *, SDWebImageDownloaderOperation *> *URLOperations;
    
    - (void)cancel:(nullable SDWebImageDownloadToken *)token {
        dispatch_barrier_async(self.barrierQueue, ^{
            SDWebImageDownloaderOperation *operation = self.URLOperations[token.url];
            BOOL canceled = [operation cancel:token.downloadOperationCancelToken];
            if (canceled) {
                [self.URLOperations removeObjectForKey:token.url];
            }
        });
    }
    

    取消任务时在SDWebImageDownloader的URLOperations属性里移除对应的operation,并实现

     [operation cancel:token.downloadOperationCancelToken];
    

    并在SDWebImageDownloaderOperation清除对应回调

    #import "SDWebImageDownloaderOperation.h"
    
    - (BOOL)cancel:(nullable id)token {
        __block BOOL shouldCancel = NO;
        dispatch_barrier_sync(self.barrierQueue, ^{
            [self.callbackBlocks removeObjectIdenticalTo:token];
            if (self.callbackBlocks.count == 0) {
                shouldCancel = YES;
            }
        });
        if (shouldCancel) {
            [self cancel];
        }
        return shouldCancel;
    }
    
    

    SDWebImageManager & SDWebImageCombinedOperation

    //SDWebImageCombinedOperation
    
    @interface SDWebImageCombinedOperation : NSObject <SDWebImageOperation>
    
    @property (assign, nonatomic, getter = isCancelled) BOOL cancelled;
    
    //这个属性在SDWebImageManager的重要方法里赋值,闭包里实现SDWebImageDownloader的cancel方法
    @property (copy, nonatomic, nullable) SDWebImageNoParamsBlock cancelBlock;
    //这个属性在SDWebImageManager的重要方法里调用`- (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDCacheQueryCompletedBlock)doneBlock `赋值,是用于取缓存时的,在取缓存时,会有一个io队列,这个cacheOperation没有其他用,只用到了他的isCanceled和cancel方法,在队列开始执行取缓存操作时判断是否已经被取消,很奇葩的方法。。。
    @property (strong, nonatomic, nullable) NSOperation *cacheOperation;
    
    @end
    
    @implementation SDWebImageCombinedOperation
    
    - (void)setCancelBlock:(nullable SDWebImageNoParamsBlock)cancelBlock {
        // check if the operation is already cancelled, then we just call the cancelBlock
        if (self.isCancelled) {
            if (cancelBlock) {
                cancelBlock();
            }
            _cancelBlock = nil; // don't forget to nil the cancelBlock, otherwise we will get crashes
        } else {
            _cancelBlock = [cancelBlock copy];
        }
    }
    
    - (void)cancel {
        self.cancelled = YES;
        if (self.cacheOperation) {
            [self.cacheOperation cancel];
            self.cacheOperation = nil;
        }
        if (self.cancelBlock) {
            self.cancelBlock();
            
            // TODO: this is a temporary fix to #809.
            // Until we can figure the exact cause of the crash, going with the ivar instead of the setter
    //        self.cancelBlock = nil;
            _cancelBlock = nil;
        }
    }
    
    @end
    
    //SDInternalCompletionBlock内部回调 用于更新图片什么的
    - (id <SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url
                                         options:(SDWebImageOptions)options
                                        progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                                       completed:(nullable SDInternalCompletionBlock)completedBlock {
        // Invoking this method without a completedBlock is pointless
        NSAssert(completedBlock != nil, @"If you mean to prefetch the image, use -[SDWebImagePrefetcher prefetchURLs] instead");
    
        // Very common mistake is to send the URL using NSString object instead of NSURL. For some strange reason, XCode won't
        // throw any warning for this type mismatch. Here we failsafe this error by allowing URLs to be passed as NSString.
        if ([url isKindOfClass:NSString.class]) {
            url = [NSURL URLWithString:(NSString *)url];
        }
    
        // Prevents app crashing on argument type error like sending NSNull instead of NSURL
        if (![url isKindOfClass:NSURL.class]) {
            url = nil;
        }
    
        __block SDWebImageCombinedOperation *operation = [SDWebImageCombinedOperation new];
        __weak SDWebImageCombinedOperation *weakOperation = operation;
    
        BOOL isFailedUrl = NO;
        if (url) {
            @synchronized (self.failedURLs) {
                isFailedUrl = [self.failedURLs containsObject:url];
            }
        }
    
        if (url.absoluteString.length == 0 || (!(options & SDWebImageRetryFailed) && isFailedUrl)) {
            [self callCompletionBlockForOperation:operation completion:completedBlock error:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil] url:url];
            return operation;
        }
    
        @synchronized (self.runningOperations) {
            [self.runningOperations addObject:operation];
        }
        NSString *key = [self cacheKeyForURL:url];
    
        //SDWebImageCombinedOperation 在缓存中寻找图片,,没有找到图片就在done回调里执行网络请求
        operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) {
            if (operation.isCancelled) {
                [self safelyRemoveOperationFromRunning:operation];
                return;
            }
    
            if ((!cachedImage || options & SDWebImageRefreshCached) && (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url])) {
                if (cachedImage && options & SDWebImageRefreshCached) {
                    //如果需要刷新缓存,先回调原来的图片,再尝试网络刷新
                    // If image was found in the cache but SDWebImageRefreshCached is provided, notify about the cached image
                    // AND try to re-download it in order to let a chance to NSURLCache to refresh it from server.
                    [self callCompletionBlockForOperation:weakOperation completion:completedBlock image:cachedImage data:cachedData error:nil cacheType:cacheType finished:YES url:url];
                }
    
                // download if no image or requested to refresh anyway, and download allowed by delegate
                SDWebImageDownloaderOptions downloaderOptions = 0;
                if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority;
                if (options & SDWebImageProgressiveDownload) downloaderOptions |= SDWebImageDownloaderProgressiveDownload;
                if (options & SDWebImageRefreshCached) downloaderOptions |= SDWebImageDownloaderUseNSURLCache;
                if (options & SDWebImageContinueInBackground) downloaderOptions |= SDWebImageDownloaderContinueInBackground;
                if (options & SDWebImageHandleCookies) downloaderOptions |= SDWebImageDownloaderHandleCookies;
                if (options & SDWebImageAllowInvalidSSLCertificates) downloaderOptions |= SDWebImageDownloaderAllowInvalidSSLCertificates;
                if (options & SDWebImageHighPriority) downloaderOptions |= SDWebImageDownloaderHighPriority;
                if (options & SDWebImageScaleDownLargeImages) downloaderOptions |= SDWebImageDownloaderScaleDownLargeImages;
                
                if (cachedImage && options & SDWebImageRefreshCached) {
                    // force progressive off if image already cached but forced refreshing
                    downloaderOptions &= ~SDWebImageDownloaderProgressiveDownload;
                    // ignore image read from NSURLCache if image if cached but force refreshing
                    downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse;
                }
                
                SDWebImageDownloadToken *subOperationToken = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
                    __strong __typeof(weakOperation) strongOperation = weakOperation;
                    if (!strongOperation || strongOperation.isCancelled) {
                        // Do nothing if the operation was cancelled
                        // See #699 for more details
                        // if we would call the completedBlock, there could be a race condition between this block and another completedBlock for the same object, so if this one is called second, we will overwrite the new data
                    } else if (error) {
                        [self callCompletionBlockForOperation:strongOperation completion:completedBlock error:error url:url];
    
                        if (   error.code != NSURLErrorNotConnectedToInternet
                            && error.code != NSURLErrorCancelled
                            && error.code != NSURLErrorTimedOut
                            && error.code != NSURLErrorInternationalRoamingOff
                            && error.code != NSURLErrorDataNotAllowed
                            && error.code != NSURLErrorCannotFindHost
                            && error.code != NSURLErrorCannotConnectToHost) {
                            @synchronized (self.failedURLs) {
                                [self.failedURLs addObject:url];
                            }
                        }
                    }
                    else {
                        if ((options & SDWebImageRetryFailed)) {
                            @synchronized (self.failedURLs) {
                                [self.failedURLs removeObject:url];
                            }
                        }
                        
                        BOOL cacheOnDisk = !(options & SDWebImageCacheMemoryOnly);
    
                        if (options & SDWebImageRefreshCached && cachedImage && !downloadedImage) {
                            // Image refresh hit the NSURLCache cache, do not call the completion block
                        } else if (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:self transformDownloadedImage:downloadedImage withURL:url];
    
                                if (transformedImage && finished) {
                                    BOOL imageWasTransformed = ![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];
                                }
                                
                                [self callCompletionBlockForOperation:strongOperation completion:completedBlock image:transformedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
                            });
                        } else {
                            if (downloadedImage && finished) {
                                [self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key toDisk:cacheOnDisk completion:nil];
                            }
                            [self callCompletionBlockForOperation:strongOperation completion:completedBlock image:downloadedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
                        }
                    }
    
                    if (finished) {
                        [self safelyRemoveOperationFromRunning:strongOperation];
                    }
                }];
                operation.cancelBlock = ^{
                    [self.imageDownloader cancel:subOperationToken];
                    __strong __typeof(weakOperation) strongOperation = weakOperation;
                    [self safelyRemoveOperationFromRunning:strongOperation];
                };
            } else if (cachedImage) {
                __strong __typeof(weakOperation) strongOperation = weakOperation;
                [self callCompletionBlockForOperation:strongOperation completion:completedBlock image:cachedImage data:cachedData error:nil cacheType:cacheType finished:YES url:url];
                [self safelyRemoveOperationFromRunning:operation];
            } else {
                // Image not in cache and download disallowed by delegate
                __strong __typeof(weakOperation) strongOperation = weakOperation;
                [self callCompletionBlockForOperation:strongOperation completion:completedBlock image:nil data:nil error:nil cacheType:SDImageCacheTypeNone finished:YES url:url];
                [self safelyRemoveOperationFromRunning:operation];
            }
        }];
    
        return operation;
    }
    
    

    `

    相关文章

      网友评论

          本文标题:SDWebImageView源码阅读

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