how can I get the data of cached images SDWebImage

不想你离开。 提交于 2019-12-04 21:24:00

问题


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


回答1:


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




回答2:


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.




回答3:


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)
        })
    }
})


来源:https://stackoverflow.com/questions/41360747/how-can-i-get-the-data-of-cached-images-sdwebimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!