how can I get the data of cached images SDWebImage

Tawfik Bouabid picture Tawfik Bouabid · Dec 28, 2016 · Viewed 12.4k times · Source

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

Answer

Kaan Dedeoglu picture Kaan Dedeoglu · Dec 28, 2016

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
}

Also make sure you import SDWebImage in your file. (If you're using Swift/Carthage, it will be import WebImage