我在我的项目中使用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
?
答案 0 :(得分:4)
好问题。答案是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
实例上下载您的图片。第二种方法是使用ImageDownloader
上的AutoPurgingImageCache
属性创建第二个UIImageView.af_sharedImageDownloader
实例。