iOS Memory spikes on CollectionView [high resolution] images

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



iOS Memory spikes on CollectionView [high resolution] images



I have a CollectionView with cells having UIImageView. Some images in the list have really high resolution of 3000 x 2000 upwards. I'm using AlamofireImage Library to show and cache images but it still has huge spikes. I tried doing


let filter = AspectScaledToFillSizeFilter(size: imageView.frame.size)
imageView.af_setImage(withURL: url, filter: filter)



This didn't have much of a change.



Is there a better way to resize/downgrade resolution on downloading an image but before displaying it as iOS memory spikes are more because of resolution than actually the image file size.





Please try and use auto release pool around the code block. Let me know if it works for you.
– iChris
Aug 10 at 12:41






@iChris I tried that but it didn't help. Still getting 1 second memory spikes till 300-400 MB.
– Akash Popat
Aug 10 at 13:04





I've deleted nuke tag because it's compositing app for VFX.
– Gigantic Mistake
Aug 11 at 13:41


nuke




1 Answer
1



Swift 4.1



This is how I resize my images. You could process them before displaying.


// Resize to ~1.5Kx2K resoultion and compress to <200KB (JPEG 0.2)
private func resizePhoto(_ originalPhoto: UIImage) -> UIImage?
var size: CGSize
let scale = UIScreen.main.scale
if originalPhoto.size.width > originalPhoto.size.height // Landscape
size = CGSize(width: 2016/scale, height: 1512/scale)
else // Portrait
size = CGSize(width: 1512/scale, height: 2016/scale)

UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
originalPhoto.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizedPhoto = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let scaledPhotoData = UIImageJPEGRepresentation(resizedPhoto!, 0.2)
//print(">>> Resized data size: (scaledPhotoData!.count)")
return UIImage(data: scaledPhotoData)





1. Wouldn't a UIImage extension been better ? 2. Any idea how much ms this would take process an image ? 3. Wouldn't this just resize all to 1.5Kx2K ?
– Akash Popat
Aug 11 at 5:15





HI @AkashPopat. Adding this to an extension is a good idea, and I could also pass the desired resolution as a param as well. In this case, I wanted to limit my image size to 1.5K to 2K pixels and the overall file size to less than 200Kbytes (they all were taken with the back side camera). Besides reducing the resolution, the compression factor (0.2) also helps reducing the file size. Re: the time to process, it is really fast. No more than a few milliseconds, and highly convenient considering the upload time of a full resolution image.
– eharo2
Aug 17 at 19:15






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard