如果尚未缓存,如何使用SDWebImageManager下载图像?

时间:2017-09-13 00:03:47

标签: ios swift caching sdwebimage

我需要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)
   }
})

2 个答案:

答案 0 :(得分:5)

更新了从缓存中提取的答案:

<div class="body">
  <div class="topNav">
  </div>
  <div class="page">
    <div class="leftNav">
    </div>
    <div class="content">
    </div>
  </div>
</div>

为了便于参考,我将在此屏幕截图中显示XCode在输入功能时显示的注释:

enter image description here

此外,SDWebImageManager.shared().loadImage(with: URL?, options: SDWebImageOptions, progress: { (Int, Int, URL?) in code }, completed: { (UIImage?, Data?, Error?, SDImageCacheType, Bool, URL?) in code }) 文件中包含的评论:

SDWebImageManager

答案 1 :(得分:3)

您可以使用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

                })