I use AlamofireImage in my project. I use someImageView.af_setImageWithURL(url)
a lot.
However at some point I need to fetch an image manually from the imageCache, because I do not want to apply it to an UIImageView.
This brings up 2 problems:
- I can access the image cache directly by creating one with
let imageCache = AutoPurgingImageCache()
. Is this the same cache as.af_setImageWithURL()
uses (Singleton)? Or is this a new one? Which is worthless in my case, because I could not profit from the prefilled cache. - Given this is the same cache: How can I fetch the correct image? I thought about
imageCache.imageForRequest(NSUrlRequest)
. Will this match on the images I downloaded before with.af_setImageWithURL()
if the NSUrlRequest does use the same NSURL?
Question 1
That is not the same cache that UIImageView
uses. If you need access the same cache, you can do so by UIImageView.af_sharedImageDownloader.imageCache
.
Question 2
You will need to use the exact same NSURLRequest
and the same ImageFilter
. If you use the same NSURLRequest
and the same ImageFilter
, the image will be directly fetched from the cache if it exists. If you are using a different ImageFilter
, then the original image will most likely be pulled from the NSURLCache
, then the ImageFilter
will be run over it, placed into the AutoPurgingImageCache
and returned. If the NSURLRequest
is different, the new image will need to be downloaded again.
This is answer to the comment: How to download multiple images, save them to cache and get from cache.
Following code will download images from array of URLs and cache them in-memory.
let imageDownloader = ImageDownloader()
var URLRequests = [URLRequestConvertible]()
URLRequests.append(NSURLRequest(URL: NSURL(string: "https://some/image/some.png")!))
As @cnoon mentioned, to set an image use myUIImageView.af_setImageWithURL(imageUrl)
This will load image from cache if present. If it doesn't exist, then network call will happen
来源:https://stackoverflow.com/questions/33318935/alamofire-image-fetch-image-from-autopurgingimagecache-after-af-setimagewithurl