AlamofireImage cache not working

I want to use AlamofireImage to download images from a URL and then cache it on the device’s disk space. I read a few posts about this and found out that AlamofireImage supports this with the ImageDownloader class. The most interesting information was given in this answer SO

So, I tried installing a custom NSURLCache for ImageDownloader so that it caches images directly to disk. I did this by setting the memory size to 0. Then I use this custom ImageDownloader to load the image. The folder that I specified for the path to the disk is created on the disk, but, unfortunately, it remains empty and the image is not cached in any way.

** Edit: ** It is important to note that cached responses are not saved in a folder in the cache directory, but in the database file next to the folder.

Can someone tell me what I'm doing wrong here? Thanks so much for reading!

func diskImageDownloader(diskSpaceMB: Int = 100) -> ImageDownloader { let diskCapacity = diskSpaceMB * 1024 * 1024 let diskCache = NSURLCache(memoryCapacity: 0, diskCapacity: diskCapacity, diskPath: "alamofireimage_disk_cache") let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.URLCache = diskCache let downloader = ImageDownloader(configuration: configuration) UIImageView.af_sharedImageDownloader = downloader return downloader } func getProfileImage(atURL url: String, onComplete: SRServiceResponse<UIImage> -> Void) { guard let imageURL = NSURL(string: url) else { // TODO: Fail here return } let request = NSURLRequest(URL: imageURL) let imageDownloader = self.diskImageDownloader() imageDownloader.downloadImage(URLRequest: request) { (response) in switch response.result { case .Success(let image): // Do something case .Failure(let error): // Do something } } } 
+6
ios swift alamofire alamofireimage
source share
2 answers

To achieve your goal, make your NSURLCache , which you use as diskCache, really configured to set your own expiration date for saved images:

 class DiskCache: NSURLCache { private let constSecondsToKeepOnDisk = 30*24*60*60 // 30 days override func storeCachedResponse(cachedResponse: NSCachedURLResponse, forRequest request: NSURLRequest) { var customCachedResponse = cachedResponse // Set custom Cache-Control for Image-Response if let response = cachedResponse.response as? NSHTTPURLResponse, let contentType = response.allHeaderFields["Content-Type"] as? String, var newHeaders = response.allHeaderFields as? [String: String] where contentType.containsString("image") { newHeaders["Cache-Control"] = "public, max-age=\(constSecondsToKeepOnDisk)" if let url = response.URL, newResponse = NSHTTPURLResponse(URL: url, statusCode: response.statusCode, HTTPVersion: "HTTP/1.1", headerFields: newHeaders) { customCachedResponse = NSCachedURLResponse(response: newResponse, data: cachedResponse.data, userInfo: cachedResponse.userInfo, storagePolicy: cachedResponse.storagePolicy) } } super.storeCachedResponse(customCachedResponse, forRequest: request) } } 

Instead of creating a new ImageDownloader every time you can reuse a shared instance to call the downloadImage method: UIImageView.af_sharedImageDownloader.downloadImage(URLRequest: request)

+4
source share

I do not do this for reputation. Just to save time with the compiler.

In Swift 3.0

 class DiskCache: URLCache { private let constSecondsToKeepOnDisk = numDaysToKeep*24*60*60 // numDaysToKeep is your own constant or hard-coded value override func storeCachedResponse(_ cachedResponse: CachedURLResponse, for request: URLRequest) { var customCachedResponse = cachedResponse // Set custom Cache-Control for Image-Response let response = cachedResponse.response as! HTTPURLResponse if let contentType = response.allHeaderFields["Content-Type"] as? String, var newHeaders = response.allHeaderFields as? [String: String], contentType.contains("image") { newHeaders["Cache-Control"] = "public, max-age=\(constSecondsToKeepOnDisk)" if let url = response.url, let newResponse = HTTPURLResponse(url: url, statusCode: response.statusCode, httpVersion: "HTTP/1.1", headerFields: newHeaders) { customCachedResponse = CachedURLResponse(response: newResponse, data: cachedResponse.data, userInfo: cachedResponse.userInfo, storagePolicy: cachedResponse.storagePolicy) } } super.storeCachedResponse(customCachedResponse, for: request) } } 
+4
source share

All Articles