Manju Basha Manju Basha - 3 months ago 20
Swift Question

Not able to cache image using method downloadImageWithURL in SDWebImageManager

I am using the code given below to download the image using

downloadImageWithURL
method and to assign the image to a
UIImageView
and cache the same image using
SDImageCache().storeImage
, but i am not able to cache the image. Am i missing anything?

Here is my code:

SDWebImageManager.sharedManager().downloadImageWithURL(profileImageURL,
options: SDWebImageOptions.HighPriority,
progress: { (min:Int, max:Int) -> Void in
})
{ (image:UIImage!, error:NSError!, cacheType:SDImageCacheType, finished:Bool, url:NSURL!) -> Void in
if (image != nil)
{
self.userProfilePic.image = image
SDImageCache.sharedImageCache().storeImage(image, forKey: "userProfilePicImage", toDisk: true)

}
}

Answer

SDWebImage has a Method sd_setImageWithURL which will download the image and save it to Cache also, you don't need to manually save that image on Cache

Try below code it will solve your problem

self.userProfilePic.sd_setImageWithURL(NSURL(string: "http://www.domain.com/path/to/image.jpg")!, placeholderImage: UIImage(named: "placeholder.png")!)
Comments