12

I am using 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 fetch an image from my CDN. I have done some tests but correct me if I am wrong, this seems to store the downloaded image in to a cache. My tests included downloading a 5mb image. The first time it took about 20 seconds, the second time was instant.

The thing I would like to know is how can I clear the cache for a specific URL/image and re-download the image?

Say for example I update a users profile pic. The image name/URL will be exactly the same but I know the image has changes as the user selected a new image from their library or camera. I know the image has been uploaded successfully to the CDN as I can see the new image in the folder directly on the CDN.

puks1978
  • 3,667
  • 11
  • 44
  • 103

6 Answers6

29

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.

cnoon
  • 16,575
  • 7
  • 58
  • 66
  • How about checking the max cache that it can store and clearing.I am experiencing that sometime,some image seems not downloading all,I am using that same function too.If you need it,I will release a new question. – Thiha Aung Jan 19 '16 at 15:45
  • 5
    For some reason, this is not working for me. I'm still getting the same image even the remote image has changed. – Ross Jul 03 '16 at 05:39
  • @TommyK, mickael, ross123 did you people resolve this issue? – Noor Ali Butt Feb 13 '17 at 13:07
  • unfortunately no, will probably be moving away from this library soon – Tommy K Feb 13 '17 at 18:55
  • This snippet worked for me. If this doesn't work for you, I suggest you open the Documents/Cache directory and monitor the disk cache, see what happens when you attempt to modify the cache. – D6mi Feb 17 '17 at 08:24
  • If you're still encountering cache issues, you should also try setting the request's cache policy, so as to ignore all cached data: **URLRequest.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData** – luvieere Mar 22 '18 at 14:53
  • Didn't work for me any of the responses here mentioned. I was able to make the app reload fresh data after playing around with device's developer tools options like 'Reset Media Services' and 'Reindex All Items'. Not sure wich one did the trick though. – Omar Jun 15 '19 at 01:25
3

Swift3 and AlamofireImage 3.x It appears removeImage does all that is needed.

        // 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)
        }
Speedy99
  • 1,691
  • 15
  • 15
3

Hope this might 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
        })
uday kiran
  • 31
  • 4
2

Swift 5:

func clearImageCache(forUrl urlString: String) {
    guard let url = URL(string: urlString) else {
        return
    }
    let imageDownloader = UIImageView.af_sharedImageDownloader
    let urlRequest = URLRequest(url: url)
    // Clear the URLRequest from the in-memory cache
    _ = imageDownloader.imageCache?.removeImage(for: urlRequest, withIdentifier: nil)
    // Clear the URLRequest from the on-disk cache
    imageDownloader.session.sessionConfiguration.urlCache?.removeCachedResponse(for: urlRequest)
}
Dmitry
  • 527
  • 5
  • 13
0

My solution for the caching issue:

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)
}
iamszabo
  • 166
  • 2
  • 8
0

The response left by @cnoon was correct. The reason it isn't working for so many of you was because you were probably using some sort of filter and passing nil as the withIdentifier parameter. I was using a circular filter in this scenario:

// 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)