How can I get cached image data SDWebImage

I use the 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 the file, and not load them again

FileManager.default.createFile(atPath: newPath, contents: Data(contentsOf:  URL(string: snapchildvalue[Constants.smiles.smileImageUrl] as! String)!), attributes: nil)

Is there any way to get cached image data

+4
source share
3 answers

SDWebImageautomatically downloads downloaded images by default. You can use SDImageCacheto extract images from the cache. For the current application session, there is a memory cache that will be faster, and there is a disk cache. Usage example:

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

+9

SDWebimage URL-. URL- , URL-. . , , .

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

, - , .

0

SDWebImage

  • SDImageCache
  • imageCache.queryDiskCache
  • , , , sd_setImage, , SDImageCache.shared().store

URL-

Something like this might be the wrong 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)
        })
    }
})
0
source

Source: https://habr.com/ru/post/1665105/


All Articles