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
Different Font Size for Different Devices in Xcode 6
Uitableviewcells with Uibutton Overlaps While Scrolling
iPhone Simulator Suddenly Started Running Very Slow
Hide Remove Separator Line If Uitableviewcells Are Empty
Programmatically Select All Text in Uitextfield
Dynamic Height for Static Table Cells with Wrapping Labels
How to Consecutively Present Two Alert Views Using Swiftui
Div Scrolling Freezes Sometimes If I Use -Webkit-Overflow-Scrolling
Coca Pod Chart Not Appearing (Swift4)
Completion Handler for Uinavigationcontroller "Pushviewcontroller:Animated"
Add Shadow on Uiview Using Swift 3
Storyboards VS. the Old Xib Way
How to Make a Function Execute Every Second in Swift
Xmppframework - Retrieve Archived Messages from Openfire Server