How to clear AlamofireImage cache setImageWithURL

I use AlamofireImage in my project quite a lot, and I use

let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")! imageView.af_setImageWithURL(URL) 

to get an image from my CDN. I have done some tests, but will correct me if I am wrong, this seems to save the loaded image in cache. My tests included uploading a 5 megabyte image. The first time it took about 20 seconds, the second time was instant.

I would like to know how can I clear the cache for a specific URL / image and reload the image?

Say, for example, I’m updating a user profile profile. The name / URL of the image will be exactly the same, but I know the image has changes as the user selected a new image from his library or camera. I know that the image was successfully uploaded to the CDN since I can see the new image in the folder directly on the CDN.

+11
source share
5 answers

You need to delete the image from the cache in memory, as well as on the cache disk on the disk. You can do it 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, URLCache can only be cleared this way on the master branch. I just clicked f35e4748 , which allows you to access the base sessionManager in ImageDownloader . This is not yet available in a real release, but should be here this week.

+25
source

Swift3 and AlamofireImage 3.x Looks like removeImage does everything it needs.

  // Clear what is in the cache, this will force a refresh to ensure fresh image is loaded next time let urlRequest = Foundation.URLRequest(url: validUrl) let imageDownloader = UIImageView.af_sharedImageDownloader if let imageCache2 = imageDownloader.imageCache { _ = imageCache2.removeImage(for: urlRequest, withIdentifier: nil) } 
+3
source

Hope this can help you:

 let URL = NSURL(string: "https://cdn.domain.com/profile/image.jpg")! let imageDownloader = UIImageView.af_sharedImageDownloader let imageCache = imageDownloader.imageCache // Setting CachePolicy as reloadIgnoringLocalCacheData so that it won't use URL Cache next time when it is hitting same URL let urlRequest = URLRequest(url: URL, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData) // Clear the Image from the in-memory cache let _ = imageCache?.removeImage(for: urlRequest, withIdentifier: nil) imageView.af_setImage(withURLRequest: urlRequest, placeholderImage: UIImage(named: "placeholder"), completion: { (response) in self.imageView.image = response.result.value }) 
+3
source

My solution for caching problem:

 func af_setImageIgnoreCache(string: String?) { guard let url = string, let nsurl = URL(string: url) else { return } let urlRequest = URLRequest(url: nsurl, cachePolicy: .reloadIgnoringCacheData) let imageDownloader = ImageDownloader.default if let imageCache = imageDownloader.imageCache as? AutoPurgingImageCache, let urlCache = imageDownloader.sessionManager.session.configuration.urlCache { _ = imageCache.removeImages(matching: urlRequest) urlCache.removeCachedResponse(for: urlRequest) } af_setImage(withURLRequest: urlRequest) } 
0
source

The answer posted by @cnoon was correct. The reason this does not work for many of you was because you probably used some kind of filter and passed zero as a parameter withIdentifier . In this scenario, I used a circular filter:

 // Clearing the cache didn't work like this imageDownloader.imageCache?.removeImage(for: urlRequest, withIdentifier: nil) // Worked when adding a CircleFilter().identifier() as 'withIdentifier' as such: imageDownloader.imageCache?.removeImage(for: urlRequest, withIdentifier: CircleFilter().identifier) 
0
source

Source: https://habr.com/ru/post/1234976/


All Articles