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

[亡魂溺海] 提交于 2019-12-04 20:27:12
ThomasRS

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 ;)

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.

  • 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.

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