我如何获取缓存图像的数据SDWebImage [英] how can I get the data of cached images SDWebImage
问题描述
我正在使用SDWebImage库在UICollectionView中缓存Web图像:
I'm using SDWebImage library to cache web images in my UICollectionView:
cell.packItemImage.sd_setImage(with: URL(string: smileImageUrl[indexPath.row]))
但是我想将缓存的图像保存在本地文件中,而不是再次下载
but I want to save the cached images locally in a file instead of downloading them again
FileManager.default.createFile(atPath: newPath, contents: Data(contentsOf: URL(string: snapchildvalue[Constants.smiles.smileImageUrl] as! String)!), attributes: nil)
有没有一种方法来获取缓存图像的数据
is there a way to get the data of cached images
推荐答案
SDWebImage
默认情况下自动缓存下载的图像.您可以使用SDImageCache
从缓存中检索图像.当前应用程序会话有一个内存缓存,这会更快,并且有磁盘缓存.用法示例:
SDWebImage
caches downloaded images automatically by default. You can use SDImageCache
to retrieve images from the cache. There is a memory cache for the current app session, which will be quicker, and there is the disk cache. Example usage:
if let image = SDImageCache.shared().imageFromDiskCache(forKey: imageURL.absoluteString) {
//use image
}
if let image = SDImageCache.shared().imageFromMemoryCache(forKey: imageURL.absoluteString) {
//use image
}
还请确保您在文件中import SDWebImage
. (如果您使用的是Swift/Carthage,它将为import WebImage
Also make sure you import SDWebImage
in your file. (If you're using Swift/Carthage, it will be import WebImage
这篇关于我如何获取缓存图像的数据SDWebImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!