We have something called intervention that we have at our backend. It provides us with a URL in which we can scale image by providing a width and height after the URL
guard let url = imageURL else { return }
var smallURL = url + "/" + "(20)" + "/" + "(20)"
So, What I do here is that I load the image with size (20x20) first and show it as blurred image and when the image with actual size is loaded I replace it with original Image.
That way it is a good UX because User does not have to wait for the image to load instead it gets a impression that image is loading.
And all these things are done in extension of image view so, you can directly call it as :
imageView.setImage(imageURL: "https://i.stack.imgur.com/GsDIl.jpg")
extension UIImageView {
func setImage(imageURL : String?,size : CGFloat = ScreenSize.width ){
guard let url = imageURL else { return }
var originalURL = url
var smallURL = url + "/" + "\(20)" + "/" + "\(20)"
let actualImageUrl = URL(string: name)
let smallURL = URL(string: smallName)
self.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)
self.yy_setImage(with: smallURL, placeholder: nil, options: .showNetworkActivity) { (image, _, _, _, _) in
guard let _ = image else { return }
let blurredImg = image?.applyBlurWithRadius(0.5, tintColor: UIColor.black.withAlphaComponent(0.4), saturationDeltaFactor: 1.0)
self.image = blurredImg
self.yy_setImage(with: actualImageUrl, placeholder: blurredImg, options: .progressiveBlur, completion: { [weak self] (image, url, type, _, _) in
self?.image = image
})
}
}
}
Instead of using SDWebImage I have used YYWebImage it provides more features and It is based on SDWebImage.
Hope it Helps!!