我在我的项目中经常使用 AlamofireImage 将图像设置为 UIImageView:
let imageView = UIImageView(frame: frame)
let URL = NSURL(string: "https://httpbin.org/image/png")!
imageView.af_setImageWithURL(URL)
在我的应用程序中的某个时刻,我需要直接获取图像,因为除了将它设置为 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)
}
}
这两种情况是否共享相同 AutoPurgingImageCache
?
最佳答案
好问题。答案是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
}
如果您希望共享同一张图片,则需要在 UIImageView.af_sharedImageDownloader
实例上下载您的图片。第二种方法是使用 UIImageView.af_sharedImageDownloader
上的 AutoPurgingImageCache
属性创建第二个 ImageDownloader
实例。
关于ios - Alamofire:af_setImageWithURL 和 ImageDownloader 是否共享同一个 AutoPurgingImageCache?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33328179/