0

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.

Isha Balla
  • 733
  • 2
  • 9
  • 25

1 Answers1

5

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.

Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50
  • Don't I have to store it to a key and then retrieve it later? Like for some cases in my application, a certain token is being appended to the url and even though the url of the image remains constant, the token varies everytime. Thus, I just need to update the key in such cases. Any Idea on this? – Isha Balla Nov 27 '17 at 06:35
  • Check updated code. There is diff in completion block. – Sharad Chauhan Nov 27 '17 at 07:01
  • @DilipTiwari any one of them you can use. But I would suggest first one. – Sharad Chauhan Jul 16 '18 at 11:17
  • @SharadChauhan what it will do if i use first because i m using sdwebimage could u explain more to load faster image – Dilip Tiwari Jul 16 '18 at 11:31
  • @DilipTiwari Both are same, in first you don't need to cache image manually, SDWebImage will do it for you and key for that cached image will be the URL of that image. But if you want to write your own logic to cache the image with custom key use second. – Sharad Chauhan Jul 16 '18 at 11:45
  • In my app there are 200 images and it takes time to load @SharadChauhan and what is "imageUrlString" in first – Dilip Tiwari Jul 16 '18 at 11:46
  • @DilipTiwari imageUrlString for what ? Use downloadedImage to display image, this is the downloaded/cached image. – Sharad Chauhan Jul 16 '18 at 11:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176056/discussion-between-dilip-tiwari-and-sharad-chauhan). – Dilip Tiwari Jul 16 '18 at 11:50