puks1978
puks1978

Reputation: 3697

How to clear AlamofireImage setImageWithURL cache

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.

Upvotes: 12

Views: 9634

Answers (6)

Dmitry
Dmitry

Reputation: 607

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

Upvotes: 2

Jovan Radivojša
Jovan Radivojša

Reputation: 1

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)

Upvotes: 0

uday kiran
uday kiran

Reputation: 31

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

Upvotes: 3

iamszabo
iamszabo

Reputation: 206

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

Upvotes: 0

Speedy99
Speedy99

Reputation: 1761

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

Upvotes: 3

cnoon
cnoon

Reputation: 16663

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.

Upvotes: 29

Related Questions