Using disk cached images if present in Alamofire images

I use the AlamofireImage library to upload uploaded images.

The code:

import UIKit import AlamofireImage class ViewController: UIViewController { @IBOutlet weak var firstImageView: UIImageView! @IBOutlet weak var secondImageView: UIImageView! let downloader = ImageDownloader() let URLRequest = NSURLRequest(URL: NSURL(string: "https://httpbin.org/image/jpeg")!) override func viewDidLoad() { super.viewDidLoad() requestFirstImage() } func requestFirstImage() { downloader.downloadImage(URLRequest: URLRequest) { response in print(response.request) print(response.response) debugPrint(response.result) if let image = response.result.value { self.firstImageView.image = image self.requestSecondImage() } } } func requestSecondImage() { downloader.downloadImage(URLRequest: URLRequest) { response in print(response.request) print(response.response) debugPrint(response.result) if let image = response.result.value { self.secondImageView.image = image } } } } 

Magazine

enter image description here

As shown in the log, the first image is requested, and the second from the cache. No further request is made and the image is displayed instantly.

I expect that when you restart the application, even the first image extracted from the cache, but Log will remain the same. I looked at Library/Caches/.../fsCachedData , and the image is there, ready to receive.

Question: What am I missing here? I need the first image to come from the disk cache on subsequent requests.

+7
ios swift alamofire alamofireimage
source share
1 answer

This approach saves image requests for as long as possible on disk, as their maximum cache management age and space are available. If you want to set the maximum age, you need to configure a custom NSURLCache as diskCache , where you need to return the modified cachedResponse to the storeCachedResponse method. By the way, the memory cache is handled by AutoPurgingImageCache in ImageDownloader . Configure your downloader using this method:

 func diskImageDownloader(diskSpaceMB: Int = 150) -> ImageDownloader { let diskCapacity = diskSpaceMB * 1024 * 1024 let diskCache = NSURLCache(memoryCapacity: 0, diskCapacity: diskCapacity, diskPath: "image_disk_cache") let configuration = NSURLSessionConfiguration.defaultSessionConfiguration() configuration.URLCache = diskCache let downloader = ImageDownloader(configuration: configuration) UIImageView.af_sharedImageDownloader = downloader return downloader } 

Updated 08/09/16 for @ kishorer747:

NSURLCache memoryCapacity is zero because I do not want responses to image requests to be cached to save memory. Only the image for the url request should be stored there as a key using AutoPurgingImageCache in memory. You can change my sample method as follows to set the desired cacheCapacity and cachePurgeCapacity for the image memory cache:

 let cacheCapacity = 100 * 1024 * 1024 let cachePurgeCapacity = 60 * 1024 * 1024 let imageCache: ImageRequestCache = AutoPurgingImageCache(memoryCapacity: cacheCapacity, preferredMemoryUsageAfterPurge: cachePurgeCapacity) let downloader = ImageDownloader(configuration: configuration, imageCache: imageCache) 
+10
source share

All Articles