I have a massive UIImage downloaded from a web resource (>300MBs) which when I attempt to render is causing an app crash due to memory. I am trying to resize the image using the following code:
+ (UIImage *)imageWithImage:(UIImage *)image scaled:(float) scale {
//UIGraphicsBeginImageContext(newSize);
CGSize size = (CGSize){scale * image.size.width, scale * image.size.height};
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
The problem is, as you may have guessed, that this requires actually rendering the image and thus causes the same crash. Is there a way to resize an large image without placing such an enormous memory strain on the system?
The problem is that you are holding the image in memory. Download it to disk and use the ImageIO framework to read a "thumbnail" (smaller size) directly from disk without ever having to hold the full-size image in memory.
You can try this one
+ (UIImage *)decodedImageWithImage:(UIImage *)image maxSize:(NSInteger) maxSize.
Code fromSDWebImage