Swift - How to Save Memory Using Alamofireimage/Imageshack

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.

Alamofire .dowload takes a lot memory (ios, swift)

The problem is with this...

UIImage(contentsOfFile: imagePath)

Specifically with the fact that you are (presumably) downloading a compressed format of your image.

However, UIImage is uncompressed in memory. So each pixel of your image will take 4 bytes of information (red, green, blue, alpha).

So if you have an image that is (for example) 5 mega pixels.

Then 5,000,000 pixels * 4b = 20MB.

So, I imagine your images are around 10MP?

The best way around this is to optimise your image download. If you're displaying an image on an iPhone then there is no point downloading a 10MP version of it. You might as well resize it to be much much smaller.

Ideally, you should be resizing the images on the backend before the download happens.

Best practice in terms of storing profile images in app

I use SDWebImage that stores images in memory/disk, one can cache image manually in image cache or it also has extension to set image via URL for UIImageView, so it downloads and caches image automatically (and call completion block if you want). It clears images those old (say a week old), or you can clear cache manually.
Kingfisher is also great and written in swift.



Related Topics



Leave a reply



Submit