luc*_*eer 3 caching ios sdwebimage swift
我需要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)
}
})
Run Code Online (Sandbox Code Playgroud)
更新了从缓存中提取的答案:
SDWebImageManager.shared().loadImage(with: URL?, options: SDWebImageOptions, progress: { (Int, Int, URL?) in
code
}, completed: { (UIImage?, Data?, Error?, SDImageCacheType, Bool, URL?) in
code
})
Run Code Online (Sandbox Code Playgroud)
为了参考起见,其中包括此屏幕快照,该屏幕快照显示了输入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
Run Code Online (Sandbox Code Playgroud)
您可以用来SDWebImageManager.shared().cachedImageExists(for: imageUrl)了解是否已URL缓存
之后就可以用这个方法获取cacheKey
let cacheKey = SDWebImageManager.shared().cacheKey(for: imageUrl)
Run Code Online (Sandbox Code Playgroud)
有了 CacheKey,您可以调用此方法,在他的闭包中您将获得缓存的图像
SDWebImageManager.shared().imageCache.queryDiskCache(forKey: cacheKey, done: { (image, cacheType) in
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2210 次 |
| 最近记录: |