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.
imageCache
usingAutoPurgingImageCache
. Can you get running the example even when offline loading from disk ? – SexistImageDownloader()
does disk cache. I can even see there atLibrary/Caches/...
, so is working fine writing the image to disk. – Sexist