Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 677
Tawfik Bouabid Avatar asked Dec 28 '16 11:12

Tawfik Bouabid


People also ask

Do images get cached?

does the browser cache images automatically? @Logan: Yes, the browser caches images automatically, provided your server sends the necessary headers to tell the browser it's safe to cache it.

What is SDWebImage in IOS?

SDWebImage Public. Asynchronous image downloader with cache support as a UIImageView category.

How do I use SDWebImage in Swift 4?

Click File -> Swift Packages -> Add Package Dependency , enter SDWebImage repo's URL. Or you can login Xcode with your GitHub account and just type SDWebImage to search. After select the package, you can choose the dependency type (tagged version, branch or commit). Then Xcode will setup all the stuff for you.


3 Answers

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.

like image 71
Saad Avatar answered Oct 31 '22 12:10

Saad


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

like image 29
Kaan Dedeoglu Avatar answered Oct 31 '22 12:10

Kaan Dedeoglu


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)
        })
    }
})
like image 3
Tj3n Avatar answered Oct 31 '22 13:10

Tj3n