I'm using the AlamofireImage library to cache downloaded images.
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
}
}
}
}
Log:
As the log shows the first image is requested and the second one is fetched from the cache. No extra request is made and the image shows instantly.
I expect when i re-launch the app that even the first image where fetched from the cache but the Log
remains the same. I looked to the Library/Caches/.../fsCachedData
and the image is there, ready to be fetched.
Question: What i'm missing here ? I need that the first image get fetched from the disk cache on subsequent requests.
This approach saves the image requests as long on the disk as their cache control max age says and space is available. If you want to set an own max age you have to set up a custom NSURLCache as diskCache
where you have to return your modified cachedResponse
in the storeCachedResponse method. By the way, the memory cache is handled by the AutoPurgingImageCache
in the ImageDownloader
.
Set up your downloader
with 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 on 08/09/16 for @kishorer747:
The NSURLCache memoryCapacity
is zero because I don't want image request responses to be saved in cache to save memory. There should only the image be saved for the request url as key by the AutoPurgingImageCache
in memory. You can modify my example method as followed to set your 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With