SDWebImage process images before caching

帅比萌擦擦* 提交于 2019-11-27 11:22:23

问题


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?


回答1:


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




回答2:


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.




回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/12928103/sdwebimage-process-images-before-caching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!