1

I use AlamofireImage in conjunction with PromiseKit and Alamofire. I use promises to chain the download of x number of images since I want to do this synchronically. I can't seem to understand if ImageDownloader() caches automatically or if I have to add the image explicitly to the cache? The only examples I've seen so far are not using the ImageDownloader so I have a real hard time finding an answer to this.

If not - how do I add it the cache? I've tried using:

self.imageDownloader.imageCache?.addImage(image, withIdentifier: imageUrl)

But all it does is increase my memory usage for all eternity(i.e. adding the same image to the cache over and over)

ClockWise
  • 1,509
  • 15
  • 32

2 Answers2

0

I think that defining an AutoPurgingImageCache() and then using it while caching process need to solve your memory usage problem.

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

You can change memoryCapacity, preferredMemoryUsageAfterPurge represent MB. To add any image in your cache, you can use like that:

photoCache.addImage(scaledImage, withIdentifier: urlString)

Also you can check this topic for more details and AlamofireImage GitHub page.

Community
  • 1
  • 1
iamburak
  • 3,508
  • 4
  • 34
  • 65
  • There's a small syntax error there. To add the image to the cache, you'd call `.add` instead of `.addImage` – bachonk Mar 17 '18 at 00:25
0

I was also using the same approach you did here. But according to the API reference the image should be added to cache automatically:

Each downloaded image is cached in the underlying NSURLCache as well as the in-memory image cache that supports image filters.

https://alamofire.github.io/AlamofireImage/Classes/ImageDownloader.html