SDWebImage源碼解讀 二(下載圖片)

說道用sdWebImage下載圖片,那就先簡單介紹下SDWebImage中下載圖片的幾個工具類了。SDWebImageManager是SDWebImage的核心類,他有兩個屬性SDImageCache *imageCache(圖片緩存)、 SDWebImageDownloader *imageDownloader(圖片下載)和一個代理id <SDWebImageManagerDelegate> delegate。

1、協議SDWebImageManagerDelegate

@protocol SDWebImageManagerDelegate <NSObject>

@optional

/**
 * Controls which image should be downloaded when the image is not found in the cache.
 * 控制在緩存中找不到圖像時應下載的圖像。
 *
 * @param imageManager The current `SDWebImageManager`  當前的“ SDWebImageManager”,也就是當前的圖片管理類
 * @param imageURL     The url of the image to be downloaded    要下載圖片的URL
 *
 * @return Return NO to prevent the downloading of the image on cache misses. If not implemented, YES is implied.
 也就是判斷緩存中有沒有圖片,控制是否去下載圖片


  這個方法就是判斷本地緩存中是否有某個url的圖片,根據是否存在返回YES/NO
 */

- (BOOL)imageManager:(nonnull SDWebImageManager *)imageManager shouldDownloadImageForURL:(nullable NSURL *)imageURL;

/**
 * Controls the complicated logic to mark as failed URLs when download error occur.
 * 控制發生下載錯誤時將複雜的邏輯標記爲失敗的URL。
 * If the delegate implement this method, we will not use the built-in way to mark URL as failed based on error code;
 * 如果委託實現此方法,則不會基於錯誤代碼使用內置方法將URL標記爲失敗;
 @param imageManager The current `SDWebImageManager` 當前的“ SDWebImageManager”
 @param imageURL The url of the image 這個圖片的URL
 @param error The download error for the url 這個url下載出現錯誤的error
 @return Whether to block this url or not. Return YES to mark this URL as failed. 是否阻止該URL。

這個方法的作用是,判斷某個url是否已經被標記過爲下載失敗,被標記失敗的url將不會被下載圖片,直接返回錯誤

 */
- (BOOL)imageManager:(nonnull SDWebImageManager *)imageManager shouldBlockFailedURL:(nonnull NSURL *)imageURL withError:(nonnull NSError *)error;


/**
 * Allows to transform the image immediately after it has been downloaded and just before to cache it on disk and memory.
 * 允許在下載圖像之後立即轉換圖像,然後再將其緩存在磁盤和內存中。
 * NOTE: This method is called from a global queue in order to not to block the main thread.
 * 注意:爲了不阻塞主線程,從全局隊列中調用此方法。
 *
 *
 * @param imageManager The current `SDWebImageManager`
 * @param image        The image to transform
 * @param imageURL     The url of the image to transform
 *
 * @return The transformed image object.
 * 
 *
 * 該方法的作用是,在下載完圖片後,首先做的是將圖片賦值,顯示到屏幕上之後再將圖片保存到緩存和磁盤中
 */
- (nullable UIImage *)imageManager:(nonnull SDWebImageManager *)imageManager transformDownloadedImage:(nullable UIImage *)image withURL:(nullable NSURL *)imageURL;

@end

2、SDImageCache

SDImageCache提供了對圖片的內存緩存、異步磁盤緩存、圖片緩存查詢等功能,下載過的圖片會被緩存到內存,也可選擇保存到本地磁盤,當再次請求相同圖片時直接從緩存中讀取圖片,從而大大提高了加載速度。在SDImageCache中,內存緩存是通過 NSCache的子類AutoPurgeCache來實現的;磁盤緩存是通過 NSFileManager 來實現文件的存儲(默認路徑爲/Library/Caches/default/com.hackemist.SDWebImageCache.default),是異步實現的。

解讀SDWebImage提供的方法

#pragma mark - Properties - 屬性

/**
 * Cache Config object - storing all kind of settings 
 * 緩存配置對象-存儲所有類型的設置
 */
@property (nonatomic, nonnull, readonly) SDImageCacheConfig *config;

/**
 * The maximum "total cost" of the in-memory image cache. The cost function is the number of pixels held in memory.
 * 內存中圖像緩存的最大“總成本”。 代價函數是內存中保留的像素數。,其實就是內存最大容量
 */
@property (assign, nonatomic) NSUInteger maxMemoryCost;

/**
 * The maximum number of objects the cache should hold.
 * 緩存應容納的最大對象數。和NSCache相關的
 */
@property (assign, nonatomic) NSUInteger maxMemoryCountLimit;

#pragma mark - Singleton and initialization - 單例和初始化

/**
 * Returns global shared cache instance 
 *
 * @return SDImageCache global instance 
 *
 * -- 返回單例
 */
+ (nonnull instancetype)sharedImageCache;

/**
 * Init a new cache store with a specific namespace - 初始化具有特定名稱空間的新緩存存儲
 *
 * @param ns The namespace to use for this cache store - 用於此緩存存儲的名稱空間
 * 
 * 使用指定的命名空間實例化一個新的緩存存儲
 */
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns;

/**
 * Init a new cache store with a specific namespace and directory - 初始化具有特定名稱空間和目錄的新緩存存儲
 *
 * @param ns        The namespace to use for this cache store - 用於此緩存存儲的名稱空間
 * @param directory Directory to cache disk images in - 用於緩存磁盤映像的目錄
*  使用指定的命名空間實例化一個新的緩存存儲和目錄
 */
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
                       diskCacheDirectory:(nonnull NSString *)directory NS_DESIGNATED_INITIALIZER;

#pragma mark - Cache paths - 緩存路徑

/**
 *  圖片緩存目錄
 */
- (nullable NSString *)makeDiskCachePath:(nonnull NSString*)fullNamespace;

/**
 * Add a read-only cache path to search for images pre-cached by SDImageCache - 添加只讀緩存路徑以搜索SDImageCache預緩存的圖像
 * Useful if you want to bundle pre-loaded images with your app - 如果您想將預加載的圖像與應用捆綁在一起,則很有用
 *
 * @param path The path to use for this read-only cache path - 用於此只讀緩存路徑的路徑
 *  添加只讀的緩存路徑  
 */
- (void)addReadOnlyCachePath:(nonnull NSString *)path;

#pragma mark - Store Ops - 商店運營

/**
 * Asynchronously store an image into memory and disk cache at the given key. - 以給定的密鑰將映像異步存儲到內存和磁盤緩存中。
 *
 * @param image           The image to store - 要存儲的圖像
 * @param key             The unique image cache key, usually it's image absolute URL - 唯一的圖片緩存鍵,通常是圖片的絕對URL
 * @param completionBlock A block executed after the operation is finished - 操作完成後執行的塊
 */
- (void)storeImage:(nullable UIImage *)image
            forKey:(nullable NSString *)key
        completion:(nullable SDWebImageNoParamsBlock)completionBlock;

/**
 * Asynchronously store an image into memory and disk cache at the given key. - 以給定的密鑰將映像異步存儲到內存和磁盤緩存中。
 *
 * @param image           The image to store
 * @param key             The unique image cache key, usually it's image absolute URL
 * @param toDisk(到磁盤)         Store the image to disk cache if YES - 如果是,將映像存儲到磁盤緩存
 * @param completionBlock A block executed after the operation is finished
 */
- (void)storeImage:(nullable UIImage *)image
            forKey:(nullable NSString *)key
            toDisk:(BOOL)toDisk
        completion:(nullable SDWebImageNoParamsBlock)completionBlock;

/**
 * Asynchronously store an image into memory and disk cache at the given key. - 以給定的密鑰將映像異步存儲到內存和磁盤緩存中。
 *
 * @param image           The image to store
 * @param imageData       The image data as returned by the server, this representation will be used for disk storage - 服務器返回的圖像數據,此表示形式將用於磁盤存儲
 *                        instead of converting the given image object into a storable/compressed image format in order - 而不是按順序將給定的圖像對象轉換爲可存儲/壓縮的圖像格式
 *                        to save quality and CPU - 節省質量和CPU
 * @param key             The unique image cache key, usually it's image absolute URL - 唯一的圖片緩存鍵,通常是圖片的絕對URL
 * @param toDisk          Store the image to disk cache if YES
 * @param completionBlock A block executed after the operation is finished
 */
- (void)storeImage:(nullable UIImage *)image
         imageData:(nullable NSData *)imageData
            forKey:(nullable NSString *)key
            toDisk:(BOOL)toDisk
        completion:(nullable SDWebImageNoParamsBlock)completionBlock;

/**
 * Synchronously store image NSData into disk cache at the given key.
 * 將映像NSData同步存儲到給定鍵的磁盤緩存中。
 *
 * @param imageData  The image data to store
 * @param key        The unique image cache key, usually it's image absolute URL
 */
- (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key;

#pragma mark - Query and Retrieve Ops - 查詢和檢索操作

/**
 *  Async check if image exists in disk cache already (does not load the image)
 *  異步檢查磁盤高速緩存中是否已存在映像(不加載映像)
 *  
 *  @param key             the key describing the url
 *  @param completionBlock the block to be executed when the check is done.
 *  @note the completion block will be always executed on the main queue
 *  完成塊將始終在主隊列上執行
 */
- (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock;

/**
 *  Sync check if image data exists in disk cache already (does not load the image)
 *  同步檢查磁盤緩存中是否已存在映像數據(不加載映像)
 *    
 *  @param key             the key describing the url 描述網址的鍵
 */
- (BOOL)diskImageDataExistsWithKey:(nullable NSString *)key;

/**
 *  Query the image data for the given key synchronously.
 *  同步查詢給定鍵的圖像數據。
 *
 *  @param key The unique key used to store the wanted image
 *  @return The image data for the given key, or nil if not found.
 *  如果找不到圖片返回nil,找的情況下返回該圖片
 */
- (nullable NSData *)diskImageDataForKey:(nullable NSString *)key;

/**
 * Operation that queries the cache asynchronously and call the completion when done.
 * 異步查詢緩存並在完成時調用完成的操作。
 *
 * @param key       The unique key used to store the wanted image
 * @param doneBlock The completion block. Will not get called if the operation is cancelled
 *
 * @return a NSOperation instance containing the cache op - 一個包含緩存操作的NSOperation實例
 * 
 *這個方法是在緩存中查詢對應key的數據
 */
- (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDCacheQueryCompletedBlock)doneBlock;

/**
 * Operation that queries the cache asynchronously and call the completion when done.
 * 異步查詢緩存並在完成時調用完成的操作。
 *
 * @param key       The unique key used to store the wanted image
 * @param options   A mask to specify options to use for this cache query
 * @param doneBlock The completion block. Will not get called if the operation is cancelled
 *
 * @return a NSOperation instance containing the cache op
 * 同上,增加了options參數
 */
- (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options done:(nullable SDCacheQueryCompletedBlock)doneBlock;

/**
 * Query the memory cache synchronously.
 * 同步查詢內存緩存。 
 *
 * @param key The unique key used to store the image
 * @return The image for the given key, or nil if not found.
 */
- (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key;

/**
 * Query the disk cache synchronously.
 * 同步查詢磁盤緩存。
 *
 * @param key The unique key used to store the image
 * @return The image for the given key, or nil if not found.
 */
- (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key;

/**
 * Query the cache (memory and or disk) synchronously after checking the memory cache.
 * 檢查內存緩存後,同步查詢緩存(內存和/或磁盤)。
 *
 * @param key The unique key used to store the image
 * @return The image for the given key, or nil if not found.
 */
- (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key;

#pragma mark - Remove Ops - 刪除操作

/**
 * Remove the image from memory and disk cache asynchronously
 * 異步從內存和磁盤緩存中刪除映像
 *
 * @param key             The unique image cache key
 * @param completion      A block that should be executed after the image has been removed (optional) 刪除圖像後應執行的Block,可選參數
 */
- (void)removeImageForKey:(nullable NSString *)key withCompletion:(nullable SDWebImageNoParamsBlock)completion;

/**
 * Remove the image from memory and optionally disk cache asynchronously
 * 從內存和可選的磁盤高速緩存中異步刪除映像
 * 
 * @param key             The unique image cache key
 * @param fromDisk        Also remove cache entry from disk if YES - 如果傳入YES,還從磁盤中刪除緩存條目
 * @param completion      A block that should be executed after the image has been removed (optional)
 */
- (void)removeImageForKey:(nullable NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(nullable SDWebImageNoParamsBlock)completion;

#pragma mark - Cache clean Ops - 緩存清理操作

/**
 * Clear all memory cached images 
 * 清除所有內存緩存的圖像
 */
- (void)clearMemory;

/**
 * Async clear all disk cached images. Non-blocking method - returns immediately.
 * 異步清除所有磁盤緩存的映像。 非阻塞方法-立即返回。
 * @param completion    A block that should be executed after cache expiration completes (optional) 可選Block參數,緩存過期後的回調
 */
- (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion;

/**
 * Async remove all expired cached image from disk. Non-blocking method - returns immediately.
 * 異步從磁盤中刪除所有過期的緩存映像。 非阻塞方法-立即返回。
 * @param completionBlock A block that should be executed after cache expiration completes (optional)
 */
- (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock;

#pragma mark - Cache Info - 緩存信息

/**
 * Get the size used by the disk cache 
 * 獲取磁盤緩存使用的大小
 */
- (NSUInteger)getSize;

/**
 * Get the number of images in the disk cache
 * 獲取磁盤緩存中的映像數
 */
- (NSUInteger)getDiskCount;

/**
 * Asynchronously calculate the disk cache's size.
 * 異步計算磁盤緩存的大小。
 */
- (void)calculateSizeWithCompletionBlock:(nullable SDWebImageCalculateSizeBlock)completionBlock;

#pragma mark - Cache Paths - 緩存路徑

/**
 *  Get the cache path for a certain key (needs the cache path root folder)
 *  根據key獲取緩存路徑(需要緩存路徑根文件夾)
 * 
 *  @param key  the key (can be obtained from url using cacheKeyForURL)
 *  @param path the cache path root folder 緩存根文件夾的路徑
 *
 *  @return the cache path
 */
- (nullable NSString *)cachePathForKey:(nullable NSString *)key inPath:(nonnull NSString *)path;

/**
 *  Get the default cache path for a certain key
 *  根據key獲取默認的緩存路徑
 *
 *  @param key the key (can be obtained from url using cacheKeyForURL)
 *
 *  @return the default cache path
 */
- (nullable NSString *)defaultCachePathForKey:(nullable NSString *)key;

3、SDWebImageDownloader
SDWebImageDownloader完成了對網絡圖片的異步下載工作,就是一個文件下載的工具類,網絡請求是在繼承於NSOperationSDWebImageDownloaderOperation類實現的。SDWebImageDownloader的主要任務是下載相關配置項的管理,包括下載隊列的先後順序、最大下載任務數量控制、下載隊列中的任務創建、取消、暫停等任務管理,以及其他 HTTPS 和 HTTP Header的設置。

  • SDWebImageDownloaderOptions:
typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
    //默認模式
    SDWebImageDownloaderLowPriority = 1 << 0,
    //本模式在返回進度Block的同時,同時返回completedBlock,裏面的UIImage就是當前下載時的圖片,可以實現將圖片一點點顯示出來的功能
    SDWebImageDownloaderProgressiveDownload = 1 << 1,
    //默認情況下,http請求阻止使用NSURLCache對象。如果設置了這個標記,則NSURLCache會被http請求使用。
    SDWebImageDownloaderUseNSURLCache = 1 << 2,
    //如果image/imageData是從NSURLCache返回的,則completion這個回調會返回nil
    SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,
    //如果app進入後臺模式,是否繼續下載,這個是通過在後臺申請時間來完成這個操作。如果指定的時間範圍內沒有完成,則直接取消下載。
    SDWebImageDownloaderContinueInBackground = 1 << 4,
    //處理緩存在`NSHTTPCookieStore`對象裏面的cookie,通過設置`NSMutableURLRequest.HTTPShouldHandleCookies = YES`來實現的。
    SDWebImageDownloaderHandleCookies = 1 << 5,
    //允許非信任的SSL證書請求。在測試的時候很有用,但是正式環境要小心使用。
    SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6,
    //默認情況下,圖片加載的順序是根據加入隊列的順序加載的。但是這個標記會把任務加入隊列的最前面。
    SDWebImageDownloaderHighPriority = 1 << 7,
    //默認情況下,圖片會按照它的原始大小來解碼顯示。這個屬性會根據設備的內存限制調整圖片的尺寸到合適的大小。如果`SDWebImageProgressiveDownload`標記被設置了,則這個flag不起作用。
    SDWebImageDownloaderScaleDownLargeImages = 1 << 8,
};

在使用的時候,當判斷options是否是SDWebImageDownloaderIgnoreCachedResponse選項時,應該這樣來判斷:

self.option & SDWebImageDownloaderIgnoreCachedResponse

SDWebImageDownloaderExecutionOrder
 

typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
    /**
     * Default value. All download operations will execute in queue style (first-in-first-out).
     * 默認值。 所有下載操作將以隊列方式(先進先出)執行。
     */
    SDWebImageDownloaderFIFOExecutionOrder,

    /**
     * All download operations will execute in stack style (last-in-first-out).
     * 所有下載操作將以堆棧樣式(後進先出)執行。
     */
    SDWebImageDownloaderLIFOExecutionOrder
};
  • SDWebImageDownloadToken
/**
 The download's URL. This should be readonly and you should not modify
 下載的URL。 只讀屬性
 */
@property (nonatomic, strong, nullable) NSURL *url;
/**
 The cancel token taken from `addHandlersForProgress:completed`. This should be readonly and you should not modify
取消令牌取自`addHandlersForProgress:completed`。 只讀屬性
 @note use `-[SDWebImageDownloadToken cancel]` to cancel the token
 */
@property (nonatomic, strong, nullable) id downloadOperationCancelToken;

SDWebImageDownloadToken爲每一個下載任務的唯一身份標識SDWebImageDownloader和我們平時開發中的下載有一些不同,它弱化了下載過程,比較強調的是下載結果,不支持斷點下載。

  • SDWebImageDownloader的屬性
/**
 * Decompressing images that are downloaded and cached can improve performance but can consume lot of memory.
 * 對下載並緩存的圖像進行解壓縮可以提高性能,但會佔用大量內存。
 * Defaults to YES. Set this to NO if you are experiencing a crash due to excessive memory consumption.
 * 默認爲是。 如果由於過多的內存消耗而導致崩潰,請將其設置爲NO。
 */
@property (assign, nonatomic) BOOL shouldDecompressImages;

/**
 *  The maximum number of concurrent downloads
 *  併發下載的最大數量
 */
@property (assign, nonatomic) NSInteger maxConcurrentDownloads;

/**
 * Shows the current amount of downloads that still need to be downloaded
 * 顯示當前仍需要下載的下載量
 */
@property (readonly, nonatomic) NSUInteger currentDownloadCount;

/**
 *  The timeout value (in seconds) for the download operation. Default: 15.0.
 *  下載操作的超時值(以秒爲單位)。 默認值:15.0。
 */
@property (assign, nonatomic) NSTimeInterval downloadTimeout;

/**
 * The configuration in use by the internal NSURLSession.
 * 內部NSURLSession使用的配置。
 * Mutating this object directly has no effect.
 *直接更改此對象無效。 
 *
 * @see createNewSessionWithConfiguration:
 * 使用配置創建新會話:
 */
@property (readonly, nonatomic, nonnull) NSURLSessionConfiguration *sessionConfiguration;


/**
 * Changes download operations execution order. Default value is `SDWebImageDownloaderFIFOExecutionOrder`.
 * 更改下載操作執行順序。 默認值爲“ SDWebImageDownloaderFIFOExecutionOrder”。
 */
@property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder;

 

4、SDWebImageManager根據文檔中的說明,就是對圖片下載的管理類

//對SDWebImageManager的一個簡單應用
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:imageURL
                      options:0
                     progress:nil
                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                        if (image) {
                            // 得到image,展示或者乾點其他的事情
    
                        }
                    }];

SDWebImageManager最主要的三個屬性在上面已經講過了,還有幾個其他的屬性和方法:


@property (weak, nonatomic) id <SDWebImageManagerDelegate> delegate;
@property (strong, nonatomic, readonly) SDImageCache *imageCache;
@property (strong, nonatomic, readonly) SDWebImageDownloader *imageDownloader;


//------------上面已經講過了---------------
/**
 * The cache filter is a block used each time SDWebImageManager need to convert an URL into a cache key. This can be used to remove dynamic part of an image URL.
 * 緩存過濾器是SDWebImageManager每次需要將URL轉換爲緩存鍵時使用的block。 這可用於刪除圖像URL的動態部分。
 * 
 * The following example sets a filter in the application delegate that will remove any query-string from the URL before to use it as a cache key:
 *下面的示例在應用程序委託中設置一個過濾器,該過濾器將在使用URL作爲緩存鍵之前從URL中刪除所有查詢字符串:
 * @code

[[SDWebImageManager sharedManager] setCacheKeyFilter:^(NSURL *url) {
    url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
    return [url absoluteString];
}];

 * @endcode
 */
@property (nonatomic, copy) SDWebImageCacheKeyFilterBlock cacheKeyFilter;

/**
 * Returns global SDWebImageManager instance.
 *
 * @return SDWebImageManager shared instance
 */
+ (SDWebImageManager *)sharedManager;

/**
 * Allows to specify instance of cache and image downloader used with image manager.
 * 允許指定與圖像管理器一起使用的緩存和圖像下載器的對象
 * @return new instance of `SDWebImageManager` with specified cache and downloader.
 */
- (instancetype)initWithCache:(SDImageCache *)cache downloader:(SDWebImageDownloader *)downloader;

/**
 * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
 * 如果圖片不在內存中,則用給定的url下載圖片,否則返回內存中的圖片
 *
 * @param url            The URL to the image
 * @param options        A mask to specify options to use for this request
 * @param progressBlock  A block called while image is downloading
 * @param completedBlock A block called when operation has been completed.
 *
 *   This parameter is required. 以上的參數是必須的
 * 
 *   This block has no return value and takes the requested UIImage as first parameter. 沒有返回值的block,返回請求得到的image作爲第一個返回值
 *   In case of error the image parameter is nil and the second parameter may contain an NSError. 返回圖片爲nil的時候,第二個參數爲error
 *
 *   The third parameter is an `SDImageCacheType` enum indicating if the image was retrieved from the local cache
 *   or from the memory cache or from the network.
 *   返回SDImageCacheType這個枚舉值,說明圖片是從服務端還是從緩存或者磁盤中獲取的
 *   The last parameter is set to NO when the SDWebImageProgressiveDownload option is used and the image is 
 *   downloading. This block is thus called repeatedly with a partial image. When image is fully downloaded, the
 *   block is called a last time with the full image and the last parameter set to YES. 
 * 重複回調的一個block,顯示下載進度,如果沒有下載完成返回NO,下載完成後返回YES
 *
 * @return Returns an NSObject conforming to SDWebImageOperation. Should be an instance of SDWebImageDownloaderOperation
 */
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url
                                         options:(SDWebImageOptions)options
                                        progress:(SDWebImageDownloaderProgressBlock)progressBlock
                                       completed:(SDWebImageCompletionWithFinishedBlock)completedBlock;

/**
 * Saves image to cache for given URL
 * 保存圖片的url
 * @param image The image to cache
 * @param url   The URL to the image
 *
 */

- (void)saveImageToCache:(UIImage *)image forURL:(NSURL *)url;

/**
 * Cancel all current operations
 * 取消當前所有操作,所有下載任務、線程、資源全部置空
 */
- (void)cancelAll;

/**
 * Check one or more operations running
 * 檢查一項或多項正在運行的操作
 */
- (BOOL)isRunning;

/**
 *  Check if image has already been cached
 *  檢查圖像是否已經被緩存
 *  @param url image url
 *
 *  @return if the image was already cached
 */
- (BOOL)cachedImageExistsForURL:(NSURL *)url;

/**
 *  Check if image has already been cached on disk only
 *  檢查圖像是否僅已緩存在磁盤上
 *  @param url image url
 *
 *  @return if the image was already cached (disk only)
 */
- (BOOL)diskImageExistsForURL:(NSURL *)url;

/**
 *  Async check if image has already been cached
 *  異步檢查圖像是否已經被緩存
 *  @param url              image url
 *  @param completionBlock  the block to be executed when the check is finished
 *  
 *  @note the completion block is always executed on the main queue
 */
- (void)cachedImageExistsForURL:(NSURL *)url
                     completion:(SDWebImageCheckCacheCompletionBlock)completionBlock;

/**
 *  Async check if image has already been cached on disk only
 *  異步檢查圖像是否被存在磁盤中
 *  @param url              image url
 *  @param completionBlock  the block to be executed when the check is finished
 *
 *  @note the completion block is always executed on the main queue
 */
- (void)diskImageExistsForURL:(NSURL *)url
                   completion:(SDWebImageCheckCacheCompletionBlock)completionBlock;


/**
 *Return the cache key for a given URL
 * 獲取url在存儲圖片的key
 */
- (NSString *)cacheKeyForURL:(NSURL *)url;

 

 

 

發佈了17 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章