Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SDWebImageManager to download image only if NOT already cached?

I need the completion handler functionality of SDWebImageManager (to set the downloaded or cached image black and white), so I'm using it instead of sd_setimage. My issue is that I can't figure out how to use SDWebImageManager to download OR get the cached version of the image. The image re-downloads each time a tableview cell is dequeued and reloaded. I tried setting options: SDWebImageDownloaderOptions.useNSURLCache, but to no avail. Any suggestions would be greatly appreciated! Here's my code:

SDWebImageManager.shared().imageDownloader?.downloadImage(with:URL(string: imgURL), options: SDWebImageDownloaderOptions.useNSURLCache, progress: nil, completed: { (image, error, cacheType, url) in 
   if image != nil {
      let beginImage = CIImage(image: image!)
      let blackNwhiteImg = beginImage?.applyingFilter("CIColorControls", withInputParameters: [kCIInputSaturationKey:0.0])
      let newImage = UIImage(ciImage: blackNwhiteImg!)
      cell.button.setImage(newImage, for: .normal)
   }
})
like image 877
lucius degeer Avatar asked Sep 13 '17 00:09

lucius degeer


2 Answers

Updated answer for pulling from cache:

SDWebImageManager.shared().loadImage(with: URL?, options: SDWebImageOptions, progress: { (Int, Int, URL?) in
    code
}, completed: { (UIImage?, Data?, Error?, SDImageCacheType, Bool, URL?) in
    code
})

For reference sake, am including this screenshot that shows the comment XCode presents while entering the function:

enter image description here

Also, the comments included in the SDWebImageManager file:

/**
 * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
 *
 * @param url            The URL to the image
 * @param options        A mask to specify options to use for this request
 * @param progressBlock  A block called while image is downloading
 *                       @note the progress block is executed on a background queue
 * @param completedBlock A block called when operation has been completed.
 *
 *   This parameter is required.
 * 
 *   This block has no return value and takes the requested UIImage as first parameter and the NSData representation as second parameter.
 *   In case of error the image parameter is nil and the third parameter may contain an NSError.
 *
 *   The forth parameter is an `SDImageCacheType` enum indicating if the image was retrieved from the local cache
 *   or from the memory cache or from the network.
 *
 *   The fith parameter is set to NO when the SDWebImageProgressiveDownload option is used and the image is
 *   downloading. This block is thus called repeatedly with a partial image. When image is fully downloaded, the
 *   block is called a last time with the full image and the last parameter set to YES.
 *
 *   The last parameter is the original image URL
 *
 * @return Returns an NSObject conforming to SDWebImageOperation. Should be an instance of SDWebImageDownloaderOperation
like image 177
CodeBender Avatar answered Sep 18 '22 23:09

CodeBender


You can use SDWebImageManager.shared().cachedImageExists(for: imageUrl) to know if an URL is cached

After that you can get the cacheKey with this method

let cacheKey = SDWebImageManager.shared().cacheKey(for: imageUrl)

Having the CacheKey you can call this method and in his closure you will get your cached image

SDWebImageManager.shared().imageCache.queryDiskCache(forKey: cacheKey, done: { (image, cacheType) in

                })
like image 24
Reinier Melian Avatar answered Sep 17 '22 23:09

Reinier Melian