Reputation: 763
So SDWebImage Download image and store to cache for key this is exactly what I want to do in my application. I want to store the image in a key and fetch image later. This is my block of code :
SDWebImageManager.shared().imageDownloader?.downloadImage(with: URL.init(string: url), options: SDWebImageDownloaderOptions.highPriority, progress: { (receivedSize:Int,expectedSize:Int,url : URL?) in
print("progressing here in this block ")
}, completed: ({(image : UIImage, data : Data,error : Error, finished : Bool) in
print("reached in the completion block ")
if finished {
SDImageCache.shared().store(image, forKey: "d", toDisk: true, completion: nil)
} else {
print(error.localizedDescription)
showAlert(viewController: self, title: "", message: error.localizedDescription)
}
} as? SDWebImageDownloaderCompletedBlock))
However, the completion block is never called.
Upvotes: 0
Views: 4303
Reputation: 4891
Use this piece of code :
SDWebImageManager.shared().loadImage(with: NSURL.init(string: individualCellData["cover_image"] as! String ) as URL?, options: .continueInBackground, progress: { (recieved, expected, nil) in
print(recieved,expected)
}, completed: { (downloadedImage, data, error, SDImageCacheType, true, imageUrlString) in
DispatchQueue.main.async {
if downloadedImage != nil{
self.yourImageView.image = downloadedImage
}
}
})
And you don't have to cache the image again as SDWebImage is doing it already. To retrieve image from cache just use the same url in this code and it will fetch the image from cache if it is there.
Updated Code:
If you want to use your own key then use imageDownloader instead of loadImage :
SDWebImageManager.shared().imageDownloader?.downloadImage(with: NSURL.init(string: individualCellData["cover_image"] as! String ) as URL?, options: .continueInBackground, progress: { (recieved, expected, nil) in
print(recieved,expected)
}, completed: { (image, data, error, true) in
print("Completed")
})
After print("Completed")
if there is no error use your caching code to cache the image with your custom key.
Upvotes: 5