How to clear AlamofireImage setImageWithURL cache

Related searches

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.

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.

ios: AlamofireImage: how to clear all image cache?, I've gone through following answers (posted in stack overflow) but none of them working. How to clear AlamofireImage setImageWithURL cache Since this library is open-source, they just share what they can. Some of them might busy. So, try use the above library that I provided you. At that library you can control your own cache (memory cache and disk cache), image downloader. So, create your own image downloader and clean the cache when you are ready to remove.

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

Alamofire/AlamofireImage: AlamofireImage is an image , Add / Remove / Fetch Images. Interacting with the ImageCache protocol APIs is very straightforward. let imageCache = AutoPurgingImageCache() let� I don't understand why the function imageView.af_setImageWithURL(URL) is not caching the results automatically or why there are no such option?. How to implement this? I tried to get cache and via fetch method to load image, but this don't work.

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

Remove all image in cache � Issue #204 � Alamofire/AlamofireImage , Hi, How can I remove all images in cache and memory after download viewcontroller? I use AlamofireImage for get image: Alamofire.request(. AlamofireImageCache. AlamofireImage + File Cache. AlamofireImage에 File Cache 할 수 있는 Method 추가 👍 기본적으로 iOS Temporary Folder에 저장하며 url.absoluteString 기준 identifier 한다.

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

swift - Wie AlamofireImage setImageWithURL Cache, Swift3 und AlamofireImage 3.x Es scheint, dass removeImage alles Notwendige ben�tigt. // Clear what is in the cache, this will force a refresh to ensure fresh� AlamofireImage. AlamofireImage is an image component library for Alamofire. Features [x] Image Response Serializers [x] UIImage Extensions for Inflation / Scaling / Rounding / CoreImage [x] Single and Multi-Pass Image Filters [x] Auto-Purging In-Memory Image Cache [x] Prioritized Queue Order Image Downloading [x] Authentication with URLCredential

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)

swift, Swift3 e AlamofireImage 3.x Appare removeImage fa tutto ci� che � necessario. // Clear what is in the cache, this will force a refresh to ensure fresh image is� Both @kcharwood and I have decided that we will NOT be directly supporting a custom on-disk cache in AlamofireImage. There are too many different expectations of how it should work and overall just makes caching much more complicated for the general user.

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

Alamofire / AlamofireImage. Watch 75 Star 3.5k // If 200 then uses data from server and saves it to NSURLCache imageView. af_setImageWithURL ETag Cache Miss.

A new tab opens displaying the Clear Browsing Data dialog. In the Obliterate the following items from drop‑down, choose the beginning of time, mark the option for "Empty the Cache", and unmark everything else. Click Clear browsing data. When it's complete, close Chrome, reopen it, and try what you were doing again.

Comments
  • 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.
  • For some reason, this is not working for me. I'm still getting the same image even the remote image has changed.
  • @TommyK, mickael, ross123 did you people resolve this issue?
  • unfortunately no, will probably be moving away from this library soon
  • 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.