Search code examples
iosimageswiftcachingalamofire

How to put image into AutoPurgingImageCache after af_setImageWithURL completed?


I want to use imageView.af_setImageWithURL(URL) together with the AutoPurgingImageCache from AlamofireImage.

How can I be informed that af_setImageWithURL fetched the image, so I can put it into my cache? Because this is done in Background.

I'd prefer something like: imageView.af_imageFromCache(URL) which sets the image from cache if it is already in there or otherwise downloads async, then sets the image and saves it in the cache.


Solution

  • The shared ImageDownloader that the UIImageView uses already puts the image into the AutoPurgingImageCache automatically. Every time you use the imageView.af_setImageWithURL(URL) it will attempt to pull the image out of the image cache if it exists. If the image is not already in the cache, it will start the download on the ImageDownloader instance.