9

I fetch a lot of images from the web, and they are all kind of sizes - they can be big, small etc..

So I can resize them when I display them in the cell but this is inefficient. It's way better to resize them after SDWebImage have download them and cache them resized, instead of storing large images on disk and resize them for every cell.

So how can I do this with SDWebImage, or I have to hack a bit onto the class?

Devfly
  • 2,495
  • 5
  • 38
  • 56

4 Answers4

26

SDWebImage developer Olivier Poitrey answered this question for me here.

You have to implement the SDWebImageManagerDelegate protocol and then set it as the shared manager's delegate like this:

SDWebImageManager.sharedManager.delegate = self;

using the imageManager:transformDownloadedImage:withURL: instance method.

More information.

Worked perfectly for me.

kameny
  • 2,372
  • 4
  • 30
  • 40
Jayson
  • 528
  • 4
  • 13
  • 1
    This is awesome! Thanks so much for pointing me into the right direction. – kleo Oct 30 '13 at 10:46
  • 1
    Sorry didn't get. I am using UIImageView+WebCache.h class's sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder method for each cell's image. What should I do, can you please tell in layman's terms. – Chanchal Raj May 01 '16 at 16:22
4

I had the same problem as you, and tried tweaking SDWebImage first, but ended up building my own component that solved the problem. You can take take a look at it here : https://github.com/adig/RemoteImageView

adig
  • 4,009
  • 1
  • 23
  • 23
  • This is great! Good job on the library. There are some annoyances, like the images reloading on every reloadData message, or images reappearing on every table scroll. You may take a look at this. Thanks and congrats! – Devfly Oct 17 '12 at 11:04
  • Thanks for the feedback. If you're referring to the animation sequence on image load you can disable that using the animate property. – adig Oct 17 '12 at 12:57
  • Hey, I submitted an issue regarding some bugs in your lib on github. You may want to take a look at it. :) – Devfly Oct 18 '12 at 11:04
0

SDWebImage 3.8.2

If using UIImageView category sd_setImageWithURL. I have created another UIImageView category (extension)

func modifiedImageFromUrl(url: NSURL?) {
    self.sd_setImageWithURL(url) { (image, error, cacheType, url) in
        if cacheType == SDImageCacheType.None && image != nil {
            dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
                let modifiedImage = // modify image as you want

                dispatch_async(dispatch_get_main_queue()) {
                    SDWebImageManager.sharedManager().saveImageToCache(modifiedImage, forURL: url)

                    self.image = modifiedImage
                }
            }
        }
    }
}
MaeSTRo
  • 355
  • 2
  • 6
  • 18
0

Expansion on MaeSTRo's answer in Swift 3:

myImageView.sd_setImage(with: imageUrl){ (image, error, cacheType, url) in
    guard let image = image, cacheType == .none else { return }

    DispatchQueue.global(qos: .userInitiated).async {
        let modifiedImage = myImageProcessor(image)
        SDWebImageManager.shared().saveImage(toCache: modifiedImage, for: imageUrl)
        DispatchQueue.main.async {
            myImageView.image = modifiedImage
            myImageView.setNeedsDisplay()
        }
    }
}
user160917
  • 9,211
  • 4
  • 53
  • 63
  • @NeerajSonaro myImagePorcessor is any function which takes an image and returns another processed image. I don't believe I need to update my answer. The "my..." is a pretty typical way of saying you need to fill in the blank here... – user160917 Mar 26 '18 at 21:02