簡體   English   中英

如果尚未緩存,如何使用SDWebImageManager下載圖像?

[英]How to use SDWebImageManager to download image only if NOT already cached?

我需要SDWebImageManager的完成處理程序功能(將下載或緩存的圖像設置為黑白),所以我使用它而不是sd_setimage。 我的問題是我無法弄清楚如何使用SDWebImageManager下載或獲取圖像的緩存版本。 每次tableview單元格出列並重新加載時,圖像都會重新下載。 我嘗試過設置選項:SDWebImageDownloaderOptions.useNSURLCache,但無濟於事。 任何建議將不勝感激! 這是我的代碼:

SDWebImageManager.shared().imageDownloader?.downloadImage(with:URL(string: imgURL), options: SDWebImageDownloaderOptions.useNSURLCache, progress: nil, completed: { (image, error, cacheType, url) in 
   if image != nil {
      let beginImage = CIImage(image: image!)
      let blackNwhiteImg = beginImage?.applyingFilter("CIColorControls", withInputParameters: [kCIInputSaturationKey:0.0])
      let newImage = UIImage(ciImage: blackNwhiteImg!)
      cell.button.setImage(newImage, for: .normal)
   }
})

從緩存中提取的更新答案:

SDWebImageManager.shared().loadImage(with: URL?, options: SDWebImageOptions, progress: { (Int, Int, URL?) in
    code
}, completed: { (UIImage?, Data?, Error?, SDImageCacheType, Bool, URL?) in
    code
})

為了便於參考,我將在此屏幕截圖中顯示XCode在輸入功能時顯示的注釋:

在此輸入圖像描述

此外, SDWebImageManager文件中包含的注釋:

/**
 * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
 *
 * @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
 *                       @note the progress block is executed on a background queue
 * @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 and the NSData representation as second parameter.
 *   In case of error the image parameter is nil and the third parameter may contain an NSError.
 *
 *   The forth parameter is an `SDImageCacheType` enum indicating if the image was retrieved from the local cache
 *   or from the memory cache or from the network.
 *
 *   The fith 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.
 *
 *   The last parameter is the original image URL
 *
 * @return Returns an NSObject conforming to SDWebImageOperation. Should be an instance of SDWebImageDownloaderOperation

您可以使用SDWebImageManager.shared().cachedImageExists(for: imageUrl)來了解是否緩存了URL

之后,您可以使用此方法獲取cacheKey

let cacheKey = SDWebImageManager.shared().cacheKey(for: imageUrl)

擁有CacheKey,您可以調用此方法,在他的閉包中,您將獲得緩存的圖像

SDWebImageManager.shared().imageCache.queryDiskCache(forKey: cacheKey, done: { (image, cacheType) in

                })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM