Tawfik Bouabid
Tawfik Bouabid

Reputation: 1292

how can I get the data of cached images SDWebImage

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

Upvotes: 11

Views: 18450

Answers (3)

Tj3n
Tj3n

Reputation: 9943

SDWebImage already have this kind of caching file locally

  1. Create a SDImageCache with namespace of your choice
  2. Try get the image with imageCache.queryDiskCache
  3. If the image exist, set it to your imageview, if not, use sd_setImage to get the image then save it to the local cache with SDImageCache.shared().store

The key usually to be the image url string

Something like this, might not be correct syntax:

imageCache.queryDiskCache(forKey: urlForImageString().absoluteString, done: {(_ image: UIImage, _ cacheType: SDImageCacheType) -> Void in
    if image {
        self.imageView.image = image
    }
    else {
        self.imageView.sd_setImage(withURL: urlForImageString(), placeholderImage: UIImage(named: "placeholder")!, completed: {(_ image: UIImage, _ error: Error, _ cacheType: SDImageCacheType, _ imageURL: URL) -> Void in
            SDImageCache.shared().store(image, forKey: urlForImageString().absoluteString)
        })
    }
})

Upvotes: 3

Kaan Dedeoglu
Kaan Dedeoglu

Reputation: 14851

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

Upvotes: 27

Saad
Saad

Reputation: 8947

SDWebimage chaches image once it is downloaded from a url. Basically it saves image against a url and next time if an image is available for a URL. It will simply get that image from cache. So the below method will be called instantly if the image is already downloaded to device.

    imgView.sd_setImage(with: URL(string:url), completed: { (image, error, type, url) in
         imgView.image = image
         //Do any thing with image here. This will be called instantly after image is downloaded to cache. E.g. if you want to save image (Which is not required for a simple image fetch, 
         //you can use FileManager.default.createFile(atPath: newPath, contents: UIImagePNGRepresentation(image), attributes: nil)
     })

Still if you want to save that image somewhere else or modify it or whatever, you can do it in the completion block above.

Upvotes: 5

Related Questions