I use AlamofireImage in my project a lot like this to set an image to an UIImageView:
let imageView = UIImageView(frame: frame)
let URL = NSURL(string: "https://httpbin.org/image/png")!
imageView.af_setImageWithURL(URL)
At some point in my app I need to get fetch an image directly, because I want to do something else than setting it to an UIImageView. AlamofireImage provides ImageDownloader
for this usecase:
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)
}
}
Do these two cases share the same AutoPurgingImageCache
?
Great question. The answer is NO
, they do not share the same AutoPurgingImageCache
.
If you take a look at the ImageDownloader
initializer, you'll see that it creates a custom AutoPurgingImageCache
automatically.
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
}
If you wish to share the same one, then you'll need to download your image on the UIImageView.af_sharedImageDownloader
instance. A second approach would be to create your second ImageDownloader
instance using the AutoPurgingImageCache
property on the UIImageView.af_sharedImageDownloader
.