Alamofireimage How to Clean Memory and Cache

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.

Swift - How to save memory using AlamofireImage/ImageShack?

You just have copy & pasted the code from AlamofireImage:

let photoCache = AutoPurgingImageCache(
memoryCapacity: 100 * 1024 * 1024,
preferredMemoryUsageAfterPurge: 60 * 1024 * 1024
)

1024 bytes x 1024 bytes x 100 will create a 100 MB in-memory cache. After that limit is reached the cache size will be reduced to 60 MB. If you do not wan't such a big cache then just reduce it. There is no "correct" number here, it depends solely on your use case. I, personally, use 20 MB in memory cache and 100 MB disk cache.

AlamofireImage is not deallacating the memory

Try to deallocate the image

imgProduct.image = nil

How is Alamofireimage saving the image to disk cache?

It is possible to save the response for the image request on disk. You can init the ImageDownloader with a modified configuration with your desired NSURLCache and set it also as the UIImageView.af_sharedImageDownloader. Here I've posted an example method for that.



Related Topics



Leave a reply



Submit