iosswiftcachingsdwebimage

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

Solution

  • 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