How to clear AlamofireImage setImageWithURL cache
You need to remove the image from the in-memory cache as well as the on-disk cache. You can do this as follows:
func clearImageFromCache() {
let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")!
let URLRequest = NSURLRequest(URL: URL)
let imageDownloader = UIImageView.af_sharedImageDownloader
// Clear the URLRequest from the in-memory cache
imageDownloader.imageCache?.removeImageForRequest(URLRequest, withAdditionalIdentifier: nil)
// Clear the URLRequest from the on-disk cache
imageDownloader.sessionManager.session.configuration.URLCache?.removeCachedResponseForRequest(URLRequest)
}
Currently, the URLCache
can only be cleared in this manner on the master
branch. I just pushed f35e4748 which allows access to the underlying sessionManager
in the ImageDownloader
. This is not yet available in an actual release yet, but should be here sometime this week.
AlamofireImage Disk Cache not working
To reach your goal make your NSURLCache
which you use as diskCache really custom to set your own expiration date for the stored 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 could reuse the shared instance to call the downloadImage
method: UIImageView.af_sharedImageDownloader.downloadImage(URLRequest: request)
Alamofire Image: Fetch Image from AutoPurgingImageCache after af_setImageWithURL()
Question 1
That is not the same cache that UIImageView
uses. If you need access the same cache, you can do so by UIImageView.af_sharedImageDownloader.imageCache
.
Question 2
You will need to use the exact same NSURLRequest
and the same ImageFilter
. If you use the same NSURLRequest
and the same ImageFilter
, the image will be directly fetched from the cache if it exists. If you are using a different ImageFilter
, then the original image will most likely be pulled from the NSURLCache
, then the ImageFilter
will be run over it, placed into the AutoPurgingImageCache
and returned. If the NSURLRequest
is different, the new image will need to be downloaded again.
How to put image into AutoPurgingImageCache after af_setImageWithURL completed?
The shared ImageDownloader
that the UIImageView
uses already puts the image into the AutoPurgingImageCache
automatically. Every time you use the imageView.af_setImageWithURL(URL)
it will attempt to pull the image out of the image cache if it exists. If the image is not already in the cache, it will start the download on the ImageDownloader
instance.
Related Topics
Multiple Bottom Sheets - the Content Doesn't Load Swiftui
For Loop Cycle Showing Always the Last Array Value Swift 3 After Http Request
Swift Enum Loses Initialized Values When Set as a Property
Create Codable Struct with Generic Type
Block Scroll Down in Scrollview - Swiftui
Uitextfield Setting Maximum Character Length in Swift
How to Validate Dynamically Added Textfields on a Button Click in Swiftui
Fibonacci Calculator Stack Overflows at 93Nd Number in Swift
Center Item Inside Horizontal Stack
How to Use Keywords as Parameter Names in Swift
How to Upload Zip Data with Alamofire
Coreplot with Swift: There Is No Yaxis.Majorintervallength
Read Data Firebase Assign Value
Swift 2 Array of Tuples Not Working as in Swift 1