Search code examples
iosswiftmemory-managementmemory-leaksalamofireimage

AlamofireImage is not deallacating the memory


I am setting an image to UITableViewCell from AlamofireImage with the extension af_setImageWithURL that's work perfectly when I scroll it too fast the memory size is increased and it does not decrease however I stop the scrolling.

I am using in UITableViewCell class:

imgProduct.image = UIImage()
clearImageFromCache(imageURL)
let filter = AspectScaledToFillSizeFilter(size: imgProduct.frame.size)
imgProduct.af_setImageWithURL(imageURL, placeholderImage: UIImage(),filter: filter)

I have also tried NSURLCache.sharedURLCache().removeAllCachedResponses() but no luck.

func clearImageFromCache(URL:NSURL) {
    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)
}

Here is the screenshot of memory debugging:

Before:

enter image description here

After scrolling:

enter image description here

NOTE:- It works perfectly when I scroll without images.


Solution

  • Try to deallocate the image

    imgProduct.image = nil