SDWebImage之SDWebImageManager


SDWebImageManager是SDWebImage的核心類。它擁有一個SDWebImageCache和一個SDWebImageDownloader屬性,分別用於圖片的緩存和下載處理。雖然是核心類,但它的源碼很簡單,這是因為相應的功能職責進行了良好的分類。下面我們來看一下它的源碼。

1.SDWebImageOptions

typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
    /**
     * By default, when a URL fail to be downloaded, the URL is blacklisted so the library won't keep trying.
     * This flag disable this blacklisting.
     */
    //默認情況下,當一個URL下載失敗的時候,這個URL會被加入黑名單列表,下次再有這個url的請求則停止請求。如果為true,這個值表示需要再嘗試請求。
    SDWebImageRetryFailed = 1 << 0,

    /**
     * By default, image downloads are started during UI interactions, this flags disable this feature,
     * leading to delayed download on UIScrollView deceleration for instance.
     */
    //默認情況下,當UI可以交互的時候就開始加載圖片。這個標記可以阻止這個時候加載。而是當UIScrollView開始減速滑動的時候開始加載。
    SDWebImageLowPriority = 1 << 1,

    /**
     * This flag disables on-disk caching
     */
    //這個屬性禁止磁盤緩存
    SDWebImageCacheMemoryOnly = 1 << 2,

    /**
     * This flag enables progressive download, the image is displayed progressively during download as a browser would do.
     * By default, the image is only displayed once completely downloaded.
     */
    //這個標記允許圖片在加載過程中顯示,就像瀏覽器那樣。默認情況下,圖片只會在加載完成以后再顯示。
    SDWebImageProgressiveDownload = 1 << 3,

    /**
     * Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
     * The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
     * This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
     * If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
     *
     * Use this flag only if you can't make your URLs static with embedded cache busting parameter.
     */
    /*
     *即使本地已經緩存了圖片,但是根據HTTP的緩存策略去網絡上加載圖片。也就是說本地緩存了也不管了,嘗試從網絡上加載數據。但是具體是從代理加載、HTTP緩存加載、還是原始服務器加載這個就根據HTTP的請求頭配置。
     *使用NSURLCache而不是SDWebImage來處理磁盤緩存。從而可能會導致輕微的性能損害。
     *這個選項專門用於處理,url地址沒有變,但是url對應的圖片數據在服務器改變的情況。
     *如果一個緩存圖片更新了,則completion這個回調會被調用兩次,一次返回緩存圖片,一次返回最終圖片。
     *我們只有在不能確保URL和它對應的內容不能完全對應的時候才使用這個標記。
     */
    SDWebImageRefreshCached = 1 << 4,

    /**
     * In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
     * extra time in background to let the request finish. If the background task expires the operation will be cancelled.
     */
    //當應用進入后台以后,圖片繼續下載。應用進入后台以后,通過向系統申請額外的時間來完成。如果時間超時,那么下載操作會被取消。
    SDWebImageContinueInBackground = 1 << 5,

    /**
     * Handles cookies stored in NSHTTPCookieStore by setting
     * NSMutableURLRequest.HTTPShouldHandleCookies = YES;
     */
    //處理緩存在`NSHTTPCookieStore`對象里面的cookie。通過設置`NSMutableURLRequest.HTTPShouldHandleCookies = YES`來實現的。
    SDWebImageHandleCookies = 1 << 6,

    /**
     * Enable to allow untrusted SSL certificates.
     * Useful for testing purposes. Use with caution in production.
     */
    //允許非信任的SSL證書請求。
    //在測試的時候很有用。但是正式環境要小心使用。
    SDWebImageAllowInvalidSSLCertificates = 1 << 7,

    /**
     * By default, images are loaded in the order in which they were queued. This flag moves them to
     * the front of the queue.
     */
    //默認情況下,圖片加載的順序是根據加入隊列的順序加載的。但是這個標記會把任務加入隊列的最前面。
    SDWebImageHighPriority = 1 << 8,
    
    /**
     * By default, placeholder images are loaded while the image is loading. This flag will delay the loading
     * of the placeholder image until after the image has finished loading.
     */
    //默認情況下,在圖片加載的過程中,會顯示占位圖。
    //但是這個標記會阻止顯示占位圖直到圖片加載完成。
    SDWebImageDelayPlaceholder = 1 << 9,

    /**
     * We usually don't call transformDownloadedImage delegate method on animated images,
     * as most transformation code would mangle it.
     * Use this flag to transform them anyway.
     */
    //默認情況下,我們不會去調用`animated images`(估計就是多張圖片循環顯示或者GIF圖片)的`transformDownloadedImage`代理方法來處理圖片。因為大部分transformation操作會對圖片做無用處理。
    //用這個標記表示無論如何都要對圖片做transform處理。
    SDWebImageTransformAnimatedImage = 1 << 10,
    
    /**
     * By default, image is added to the imageView after download. But in some cases, we want to
     * have the hand before setting the image (apply a filter or add it with cross-fade animation for instance)
     * Use this flag if you want to manually set the image in the completion when success
     */
    //默認情況下,圖片在下載完成以后都會被自動加載到UIImageView對象上面。但是有時我們希望UIImageView加載我們手動處理以后的圖片。
    //這個標記允許我們在completion這個Block中手動設置處理好以后的圖片。
    SDWebImageAvoidAutoSetImage = 1 << 11,
    
    /**
     * By default, images are decoded respecting their original size. On iOS, this flag will scale down the
     * images to a size compatible with the constrained memory of devices.
     * If `SDWebImageProgressiveDownload` flag is set the scale down is deactivated.
     */
    //默認情況下,圖片會按照它的原始大小來解碼顯示。根據設備的內存限制,這個屬性會調整圖片的尺寸到合適的大小再解碼。
    //如果`SDWebImageProgressiveDownload`標記被設置了,則這個flag不起作用。
    SDWebImageScaleDownLargeImages = 1 << 12
};

2.宏定義

/**
 圖片下載完成回調Block(外部使用,一般在我們常使用的視圖分類方法中用)

 @param image 圖片
 @param error 錯誤信息
 @param cacheType 圖片獲取方式(本地緩存;內存緩存;網絡)
 @param imageURL 圖片URL
 */
typedef void(^SDExternalCompletionBlock)(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL);

/**
 圖片下載完成回調Block(內部使用,僅在本類中使用)
 
 @param image 圖片,如果請求出錯,那么image參數為nil
 @param data 圖片數據
 @param error 錯誤信息
 @param cacheType 圖片獲取方式(本地緩存;內存緩存;網絡)
 @param finished 是否下載完成
 @param imageURL 圖片URL
 */
typedef void(^SDInternalCompletionBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL);

/**
 對URL格式做處理,在方法cacheKeyForURL:中使用

 @param url URL
 @return 處理之后的URL
 */
typedef NSString * _Nullable (^SDWebImageCacheKeyFilterBlock)(NSURL * _Nullable url);

3.協議SDWebImageManagerDelegate

@protocol SDWebImageManagerDelegate <NSObject>

@optional

/**
 在緩存中沒有找到圖片,控制是否去下載圖片

 @param imageManager SDWebImageManager
 @param imageURL 圖片URL
 @return YES/NO
 */
- (BOOL)imageManager:(nonnull SDWebImageManager *)imageManager shouldDownloadImageForURL:(nullable NSURL *)imageURL;

/**
 圖片下載完成,在保存到磁盤和內存之前,對圖片進行轉換(主要是針對動態圖)

 @param imageManager SDWebImageManager
 @param image 圖片
 @param imageURL 圖片URL
 @return 轉換之后的圖片
 */
- (nullable UIImage *)imageManager:(nonnull SDWebImageManager *)imageManager transformDownloadedImage:(nullable UIImage *)image withURL:(nullable NSURL *)imageURL;

@end

4.SDWebImageCombinedOperation

SDWebImageCombinedOperation是對每一個下載任務的封裝,它最重要的是提供了一個取消任務功能

@interface SDWebImageCombinedOperation : NSObject <SDWebImageOperation>

@property (assign, nonatomic, getter = isCancelled) BOOL cancelled;  //!<用於判斷Operation是否已經取消
@property (copy, nonatomic, nullable) SDWebImageNoParamsBlock cancelBlock;  //!<取消回調的Block
@property (strong, nonatomic, nullable) NSOperation *cacheOperation;  //!<NSOperation對象。可以通過這個屬性取消一個NSOperation

@end

@implementation SDWebImageCombinedOperation

/**
 取消Operation的回調Block

 @param cancelBlock 回調Block
 */
- (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];
    }
}

/**
 這個方法繼承自`SDWebImageOperation`協議,方法里面會調用`SDWebImageDownlaoderOperation`或者`NSOperation`的cancel方法
 */
- (void)cancel {
    self.cancelled = YES;
    if (self.cacheOperation) {
        //調用`SDWebImageDownlaoderOperation`或者`NSOperation`的cancel方法
        [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

5.SDWebImageManager的屬性

//.h文件
@property (weak, nonatomic, nullable) id <SDWebImageManagerDelegate> delegate;  //!<代理

@property (strong, nonatomic, readonly, nullable) SDImageCache *imageCache;  //!<緩存對象
@property (strong, nonatomic, readonly, nullable) SDWebImageDownloader *imageDownloader;  //!<下載對象

//.m文件
@property (strong, nonatomic, readwrite, nonnull) SDImageCache *imageCache;  //!<重寫為可讀寫的緩存對象
@property (strong, nonatomic, readwrite, nonnull) SDWebImageDownloader *imageDownloader;  //!<重寫為可讀寫的下載對象
@property (strong, nonatomic, nonnull) NSMutableSet<NSURL *> *failedURLs;  //!<加載失敗的URL
@property (strong, nonatomic, nonnull) NSMutableArray<SDWebImageCombinedOperation *> *runningOperations;  //!<當前正在加載的任務

6.SDWebImageManager的方法

6.1初始化

+ (nonnull instancetype)sharedManager {
    static dispatch_once_t once;
    static id instance;
    dispatch_once(&once, ^{
        instance = [self new];
    });
    return instance;
}

- (nonnull instancetype)init {
    SDImageCache *cache = [SDImageCache sharedImageCache];
    SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
    return [self initWithCache:cache downloader:downloader];
}

/**
 初始化

 @param cache SDImageCache對象
 @param downloader SDWebImageDownloader對象
 @return 返回初始化結果
 */
- (nonnull instancetype)initWithCache:(nonnull SDImageCache *)cache downloader:(nonnull SDWebImageDownloader *)downloader {
    if ((self = [super init])) {
        _imageCache = cache;
        _imageDownloader = downloader;
        //用於保存加載失敗的url集合
        _failedURLs = [NSMutableSet new];
        //用於保存當前正在加載的Operation
        _runningOperations = [NSMutableArray new];
    }
    return self;
}

6.2緩存查詢

/**
 根據url獲取url對應的緩存key。如果有實現指定的url轉換key的Block,則用這個方式轉換為key;否則直接用url的絕對路徑作為key

 @param url url
 @return 緩存的key
 */
- (nullable NSString *)cacheKeyForURL:(nullable NSURL *)url {
    if (!url) {
        return @"";
    }

    if (self.cacheKeyFilter) {
        return self.cacheKeyFilter(url);
    } else {
        return url.absoluteString;
    }
}

/**
 url的緩存是否存在

 @param url 緩存數據對應的url
 @param completionBlock 緩存結果回調
 */
- (void)cachedImageExistsForURL:(nullable NSURL *)url
                     completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock {
    NSString *key = [self cacheKeyForURL:url];
    //內存里面是否有key的緩存
    BOOL isInMemoryCache = ([self.imageCache imageFromMemoryCacheForKey:key] != nil);
    //內存緩存
    if (isInMemoryCache) {
        // making sure we call the completion block on the main queue
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completionBlock) {
                completionBlock(YES);
            }
        });
        return;
    }
    //磁盤緩存
    [self.imageCache diskImageExistsWithKey:key completion:^(BOOL isInDiskCache) {
        // the completion block of checkDiskCacheForImageWithKey:completion: is always called on the main queue, no need to further dispatch
        if (completionBlock) {
            completionBlock(isInDiskCache);
        }
    }];
}

/**
 url是否有磁盤緩存數據

 @param url url
 @param completionBlock 緩存結果回調
 */
- (void)diskImageExistsForURL:(nullable NSURL *)url
                   completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock {
    NSString *key = [self cacheKeyForURL:url];
    
    [self.imageCache diskImageExistsWithKey:key completion:^(BOOL isInDiskCache) {
        // the completion block of checkDiskCacheForImageWithKey:completion: is always called on the main queue, no need to further dispatch
        if (completionBlock) {
            completionBlock(isInDiskCache);
        }
    }];
}

6.3下載(SDWebImage的核心方法)

/**
 核心方法。UIView、UIImageView等分類都默認通過調用這個方法來獲取數據

 @param url 圖片的url地址
 @param options 獲取圖片加載處理的屬性
 @param progressBlock 加載進度回調
 @param completedBlock 加載完成回調
 @return 返回一個加載的載體對象,以便提供給后面取消刪除等
 */
- (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
    //如果想預先下載圖片,使用[SDWebImagePrefetcher prefetchURLs]取代本方法。預下載圖片是有很多種使用場景的,當我們使用SDWebImagePrefetcher下載圖片后,之后使用該圖片時就不用從網絡上下載了。
    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.
    //如果傳入的url是NSString格式的,則轉換為NSURL類型再處理
    if ([url isKindOfClass:NSString.class]) {
        url = [NSURL URLWithString:(NSString *)url];
    }

    // Prevents app crashing on argument type error like sending NSNull instead of NSURL
    //如果url不是NSURL類型的對象,則置為nil
    if (![url isKindOfClass:NSURL.class]) {
        url = nil;
    }
    //圖片加載獲取獲取過程中綁定一個`SDWebImageCombinedOperation`對象,以方便后續再通過這個對象對url的加載控制。
    __block SDWebImageCombinedOperation *operation = [SDWebImageCombinedOperation new];
    __weak SDWebImageCombinedOperation *weakOperation = operation;

    BOOL isFailedUrl = NO;
    //當前url是否在失敗url的集合里面。在圖片的下載中,會有一些下載失敗的情況,這時候會把這些下載失敗的url放到一個集合中去,也就是加入了黑名單,默認是不會再繼續下載黑名單中的url了,但是也有例外,當options被設置為SDWebImageRetryFailed的時候,會嘗試進行重新下載。
    if (url) {
        @synchronized (self.failedURLs) {
            isFailedUrl = [self.failedURLs containsObject:url];
        }
    }
    //如果url長度為0 或者是 失敗的url且圖片加載處理屬性不包含SDWebImageRetryFailed,則直接返回
    if (url.absoluteString.length == 0 || (!(options & SDWebImageRetryFailed) && isFailedUrl)) {
        //構建回調Block
        [self callCompletionBlockForOperation:operation completion:completedBlock error:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil] url:url];
        return operation;
    }
    //把加載圖片的operation存入runningOperations,里面是所有正在做圖片加載過程的operation的集合
    @synchronized (self.runningOperations) {
        [self.runningOperations addObject:operation];
    }
    //根據url獲取url對應的key
    NSString *key = [self cacheKeyForURL:url];
    //key為空或者圖片從內存中加載,則返回的cacheOperation是nil;其他情況cacheOperation為NSOperation對象
    operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) {
        //如果已經取消了操作,則直接返回並且移除對應的opetation對象
        if (operation.isCancelled) {
            [self safelyRemoveOperationFromRunning:operation];
            return;
        }
        //(如果未能在緩存中找到圖片||強制刷新緩存) && (代理中未實現圖片是否下載的方法||代理中圖片是否應該下載方法返回值為YES
        if ((!cachedImage || options & SDWebImageRefreshCached) && (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url])) {
            //如果從緩存中獲取了圖片並且設置了SDWebImageRefreshCached來忽略緩存,則先把緩存的圖片返回
            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
            //把圖片加載的`SDWebImageOptions`類型枚舉轉換為圖片下載的`SDWebImageDownloaderOptions`類型的枚舉
            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;
            //如果設置了強制刷新緩存的選項。則`SDWebImageDownloaderProgressiveDownload`選項失效並且添加`SDWebImageDownloaderIgnoreCachedResponse`選項
            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) {
                    //如果加載出錯,則直接返回回調,並且添加到failedURLs中
                    [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
                        && error.code != NSURLErrorNetworkConnectionLost) {
                        @synchronized (self.failedURLs) {
                            [self.failedURLs addObject:url];
                        }
                    }
                }
                else { //網絡圖片下載成功
                    //如果有重試失敗下載的選項,則把url從failedURLS中移除
                    if ((options & SDWebImageRetryFailed)) {
                        @synchronized (self.failedURLs) {
                            [self.failedURLs removeObject:url];
                        }
                    }
                    //是否需要保存到磁盤
                    BOOL cacheOnDisk = !(options & SDWebImageCacheMemoryOnly);
                    //如果設置了強制刷新緩存 && 緩存圖片存在 && 下載圖片不存在,那么直接返回,不執行回調Block
                    if (options & SDWebImageRefreshCached && cachedImage && !downloadedImage) {
                        // Image refresh hit the NSURLCache cache, do not call the completion block
                    }
                    //如果成功下載圖片 && 圖片是動態圖片 && 實現了imageManager:transformDownloadedImage:withURL:代理方法,則進行圖片的處理
                    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), ^{
                            //獲取transform以后的圖片
                            UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url];
                            //存儲transform以后的的圖片
                            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];
                }
            }];
            //重置cancelBlock,取消下載operation
            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;
}

6.4其他方法

/**
 保存圖片到緩存

 @param image 圖片
 @param url URL
 */
- (void)saveImageToCache:(nullable UIImage *)image forURL:(nullable NSURL *)url {
    if (image && url) {
        NSString *key = [self cacheKeyForURL:url];
        [self.imageCache storeImage:image forKey:key toDisk:YES completion:nil];
    }
}

/**
 取消所有的下載
 */
- (void)cancelAll {
    @synchronized (self.runningOperations) {
        NSArray<SDWebImageCombinedOperation *> *copiedOperations = [self.runningOperations copy];
        [copiedOperations makeObjectsPerformSelector:@selector(cancel)];
        [self.runningOperations removeObjectsInArray:copiedOperations];
    }
}

/**
 查看是否正在執行任務

 @return YES/NO
 */
- (BOOL)isRunning {
    BOOL isRunning = NO;
    @synchronized (self.runningOperations) {
        isRunning = (self.runningOperations.count > 0);
    }
    return isRunning;
}

/**
 安全移除任務

 @param operation 任務
 */
- (void)safelyRemoveOperationFromRunning:(nullable SDWebImageCombinedOperation*)operation {
    @synchronized (self.runningOperations) {
        if (operation) {
            [self.runningOperations removeObject:operation];
        }
    }
}

/**
 拼接回調Block並進行回調

 @param operation 任務
 @param completionBlock 回調Block
 @param error error
 @param url URL
 */
- (void)callCompletionBlockForOperation:(nullable SDWebImageCombinedOperation*)operation
                             completion:(nullable SDInternalCompletionBlock)completionBlock
                                  error:(nullable NSError *)error
                                    url:(nullable NSURL *)url {
    [self callCompletionBlockForOperation:operation completion:completionBlock image:nil data:nil error:error cacheType:SDImageCacheTypeNone finished:YES url:url];
}

/**
 拼接回調Block並進行回調

 @param operation 任務
 @param completionBlock 回調Block
 @param image 圖片
 @param data 圖片數據
 @param error error
 @param cacheType 緩存類型
 @param finished 是否完成
 @param url URL
 */
- (void)callCompletionBlockForOperation:(nullable SDWebImageCombinedOperation*)operation
                             completion:(nullable SDInternalCompletionBlock)completionBlock
                                  image:(nullable UIImage *)image
                                   data:(nullable NSData *)data
                                  error:(nullable NSError *)error
                              cacheType:(SDImageCacheType)cacheType
                               finished:(BOOL)finished
                                    url:(nullable NSURL *)url {
    dispatch_main_async_safe(^{
        if (operation && !operation.isCancelled && completionBlock) {
            completionBlock(image, data, error, cacheType, finished, url);
        }
    });
}

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM