在iPhone上调整图像大小,旋转和裁剪图像时,内存消耗会激增 [英] Memory consumption spikes when resizing, rotating and cropping images on iPhone

查看:300
本文介绍了在iPhone上调整图像大小,旋转和裁剪图像时,内存消耗会激增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageManipulator类,可以在iPhone上执行一些裁剪,调整大小和旋转相机图像。

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.

我已设法将问题隔离到我检查的代码的一部分当前图像方向属性并相应地将其旋转到 UIImageOrientationUp 。然后我从位图上下文中获取图像并将其保存到磁盘。

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.

这是我正在做的事情:

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

即使在尝试将图像缩小到大小的一半甚至1/4之后,我仍然看到尖峰,我想知道是否有一种不同/更有效的方法来完成上述旋转?

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?

提前感谢您的回复。
Rog

Thanks in advance for the replies. Rog

推荐答案

如果您保存为JPEG,我想另一种方法是按原样保存图像然后通过操作EXIF元数据将旋转设置为您想要的任何值?例如,参见这篇文章。简单但可能有效,即使您必须将图像有效负载字节保存在内存中;)

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

这篇关于在iPhone上调整图像大小,旋转和裁剪图像时,内存消耗会激增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆