Memory consumption spikes when resizing, rotating and cropping images on iPhone

女生的网名这么多〃 提交于 2019-12-09 23:34:22

问题


I have an "ImageManipulator" class that performs some cropping, resizing and rotating of camera images on the iPhone.

At the moment, everything works as expected but I keep getting a few huge spikes in memory consumption which occasionally cause the app to crash.

I have managed to isolate the problem to a part of the code where I check for the current image orientation property and rotate it accordingly to UIImageOrientationUp. I then get the image from the bitmap context and save it to disk.

This is currently what I am doing:

CGAffineTransform transform = CGAffineTransformIdentity;
// Check for orientation and set transform accordingly...
transform = CGAffineTransformTranslate(transform, self.size.width, 0);
transform = CGAffineTransformScale(transform, -1, 1);

// Create a bitmap context with the image that was passed so we can perform the rotation
CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
                                         CGImageGetBitsPerComponent(self.CGImage), 0,
                                         CGImageGetColorSpace(self.CGImage),
                                         CGImageGetBitmapInfo(self.CGImage));

// Rotate the context
CGContextConcatCTM(ctx, transform);

// Draw the image into the context
CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);

// Grab the bitmap context and save it to the disk...

Even after trying to scale the image down to half or even 1/4 of the size, I am still seeing the spikes to I am wondering if there is a different / more efficient way to get the rotation done as above?

Thanks in advance for the replies. Rog


回答1:


If you are saving to JPEG, I guess an alternative approach is to save the image as-is and then set the rotation to whatever you'd like by manipulating the EXIF metadata? See for example this post. Simple but probably effective, even if you have to hold the image payload bytes in memory ;)




回答2:


Things you can do:

  1. Scale down the image even more (which you probably don't want)

  2. Remember to release everything as soon as you finish with it

  3. Live with it

I would choose option 2 and 3.

Image editing is very resource intensive, as it loads the entire raw uncompressed image data into the memory for processing. This is inevitable as there is absolutely no other way to modify an image other than to load the complete raw data into the memory. Having memory consumption spikes doesn't really matter unless the app receives a memory warning, in that case quickly get rid of everything before it crashes. It is very rare that you would get a memory warning, though, because my app regularly loads a single > 10 mb file into the memory and I don't get a warning, even on older devices. So you'll be fine with the spikes.




回答3:


  • Have you tried checking for memory leaks and analyzing allocations?
  • If the image is still too big, try rotating the image in pieces instead of as a whole.



回答4:


As Anomie mentioned, CGBitmapContextCreate creates a context. We should release that by using

CGContextRelease(ctx);

If you have any other objects created using create or copy, that should also be released. If it is CFData, then

CFRelease(cfdata);


来源:https://stackoverflow.com/questions/6542262/memory-consumption-spikes-when-resizing-rotating-and-cropping-images-on-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!