This is how I set images to UIImageView from a web url:
import UIKit
extension UIImageView {
private var storage: Storageable {
Assembler.shared.resolve(Storageable.self)!
}
private var imageDownloader: ImageDownloaderable {
Assembler.shared.resolve(ImageDownloaderable.self)!
}
// MARK: - Internal
func setImage(with url: URL) {
downloadImage(with: url) { [weak self] image in
self?.image = image
}
}
func downloadImage(with url: URL, completion: @escaping ImageHandler) {
imageDownloader.setAuthorization(withToken: storage.bearerToken)
sd_setImage(with: url, placeholderImage: nil,
options: [.allowInvalidSSLCertificates, .avoidAutoSetImage]) { image, _, _, _ in
completion(image)
}
}
}
How do I tell SDWebImage library to downsample an image to specific size, to reduce memory usage. Is it possible?
From SDWebImage's Wiki on Github, you can use Image Transformers to do some image processing on the downloaded image.
let transformer = SDImageResizingTransformer(size: CGSize(300, 300), scaleMode: .fill)
let imageView: UIImageView
imageView.sd_setImage(with: url, placeholderImage: nil, context: [.imageTransformer: transformer])