使用 SDWebImage 获取图像时,有没有办法设置下采样选项?

Is there a way to set up downsampling option when fetching an image with SDWebImage?

这就是我将图像从网络设置到 UIImageView 的方式 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)
        }
    }
}

我如何告诉 SDWebImage 库将图像缩减采样到特定大小,以减少内存使用。可能吗?

SDWebImage's Wiki 到 Github,您可以使用 Image Transformers 对下载的图像进行一些图像处理。

let transformer = SDImageResizingTransformer(size: CGSize(300, 300), scaleMode: .fill)
let imageView: UIImageView
imageView.sd_setImage(with: url, placeholderImage: nil, context: [.imageTransformer: transformer])