I am trying to find out if it's possible to force an image to be saved on disk with Alamofireimage. Looking at the source code, I cannot locate where it is actually saving the image to the disk. Does anyone know?
Asked
Active
Viewed 4,148 times
4
-
It relies upon `NSURLSession` which uses `NSURLCache`, which transparently caches to both memory and to disk (subject to (a) size of item being downloaded (items larger than 5% of the total cache size are not cached); and (b) the HTTP headers of response). – Rob Jun 01 '16 at 09:50
-
Thanks for the reply. That's what I suspect too, but I created a custom NSURLCache and call setSharedCache during app startup, but none of the functions of custom NSURLCache are called when Alamofireimage is used to save an image. – dickyj Jun 01 '16 at 10:04
-
And are you sure your images are getting cached to persistent storage at all? Depending upon the headers or size of the images, it might not be caching. I'd check that first. All I know is that I once created a AFNetworking issue (from the same author) offering to integrate persistent storage cache, and he declined the offer and went on about how he thought apps shouldn't be caching themselves, but rather rely on the `NSURLCache` behavior. (I actually disagree with him on that point ... an app should cache whatever it jolly well wants, but it's his library and he can do what he wants.) – Rob Jun 01 '16 at 10:23
-
FWIW, I glanced at the source, and I see that he has implemented his own memory image cache (which is funny that it's neither `NSURLCache`-based nor `NSCache`-based, but I don't see any persistent storage caching (other than what is offered transparently by `NSURLSession`/`NSURLCache`. – Rob Jun 01 '16 at 10:27
-
I used this before downloading the image: mutableURLRequest.cachePolicy = NSURLRequestCachePolicy.ReturnCacheDataElseLoad, which I suspect its not really working. Any suggestions on alternatives to Alamofireimage that is better? – dickyj Jun 01 '16 at 10:34
-
You can try DFImageManager or Kingfisher. I can't vouch for them, but they're names I've seen bandied about. I used to recommend SDWebImage (even though it's Objective-C, it used to be quite good), but it had a bunch of sloppy changes to their caching logic in the last year or so and although I suspect it's remedied by now, it undermined my confidence in it. – Rob Jun 01 '16 at 10:58
-
Ok thx for the advice. I am trying out HanekeSwift, looking at the code it looks simple enough... – dickyj Jun 01 '16 at 12:58
1 Answers
5
It is possible to save the response for the image request on disk. You can init the ImageDownloader
with a modified configuration with your desired NSURLCache and set it also as the UIImageView.af_sharedImageDownloader. Here I've posted an example method for that.