如何使用SDWebImageManager仅在未缓存时下载图像?

7

我需要使用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个回答

9

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

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

为了参考,我包含了这张截图,它展示了在输入函数时XCode所呈现的注释:

enter image description here

另外,在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

非常感谢您的回复。这正好和我发布的代码一样。看起来您的代码和我的代码都在访问同一个 .downloadImage() 函数。您能否用更具体的语法再解释一下?有没有我没有设置的选项?我尝试过使用选项和不使用选项:SDWebImageDownloaderOptions.useNSURLCache。再次感谢。 - lucius degeer
我情况的另一个细节是,我注意到在已出列的tableview单元格中重新加载图像。但从我对sdwebimage的理解来看,已出列并重新加载的单元格应该与缓存的图像无缝地重新加载。 - lucius degeer
每次加载图像时,我都会在完成块中打印cacheType值。它每次都是nil。这似乎表明没有缓存图像? - lucius degeer
@DanielGrigsby,我更新了我的答案,我从错误的部分提取了代码并进行了更新,并包括了一些支持文档。 - CodeBender
太棒了。非常感谢! - lucius degeer

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

                })

1
谢谢您的回复。那么我该如何在完成块内获取该URL的缓存图像呢?据我所知,并没有传递“image”值。 - lucius degeer
1
谢谢你的回答。了解CacheKey非常有用。我认为.loadImage就是我在寻找的东西。 - lucius degeer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接