Alamofire:af_setImageWithURL 和 ImageDownloader 共享相同的 AutoPurgingImageCache 吗?

fun*_*len 3 caching uiimageview ios swift alamofire

我在项目中使用 AlamofireImage 来将图像设置为 UIImageView:

let imageView = UIImageView(frame: frame)
let URL = NSURL(string: "https://httpbin.org/image/png")!

imageView.af_setImageWithURL(URL)
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中的某个时刻,我需要直接获取图像,因为我想做一些其他事情,而不是将其设置为 UIImageView。AlamofireImage 提供了ImageDownloader此用例:

let downloader = ImageDownloader()
let URLRequest = NSURLRequest(URL: NSURL(string: "https://httpbin.org/image/jpeg")!)

downloader.downloadImage(URLRequest: URLRequest) { response in
    print(response.request)
    print(response.response)
    debugPrint(response.result)

    if let image = response.result.value {
        print(image)
    }
}
Run Code Online (Sandbox Code Playgroud)

这两个案例有相同之处 AutoPurgingImageCache吗?

cno*_*oon 5

很好的问题。答案是NO,它们并不相同AutoPurgingImageCache

如果您查看ImageDownloader初始化程序,您会发现它AutoPurgingImageCache自动创建了一个自定义。

public init(
    configuration: NSURLSessionConfiguration = ImageDownloader.defaultURLSessionConfiguration(),
    downloadPrioritization: DownloadPrioritization = .FIFO,
    maximumActiveDownloads: Int = 4,
    imageCache: ImageRequestCache? = AutoPurgingImageCache())
{
    self.sessionManager = Alamofire.Manager(configuration: configuration)
    self.sessionManager.startRequestsImmediately = false

    self.downloadPrioritization = downloadPrioritization
    self.maximumActiveDownloads = maximumActiveDownloads
    self.imageCache = imageCache
}
Run Code Online (Sandbox Code Playgroud)

如果您希望共享同一个映像,则需要在UIImageView.af_sharedImageDownloader实例上下载您的映像。第二种方法是ImageDownloader使用AutoPurgingImageCache.UIImageView.af_sharedImageDownloader