iphone - 在 iPhone 上调整图像大小、旋转和裁剪图像时内存消耗激增

标签 iphone ios core-graphics cgcontext

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

目前,一切都按预期进行,但内存消耗不断出现一些巨大的峰值,这偶尔会导致应用程序崩溃。

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

这就是我目前正在做的事情:

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 后,我仍然看到尖峰,我想知道是否有不同/更有效的方法来完成上述旋转?

预先感谢您的回复。 罗格

最佳答案

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

关于iphone - 在 iPhone 上调整图像大小、旋转和裁剪图像时内存消耗激增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6542262/

相关文章:

xcode - 在 Swift 中创建自定义 View 时,如何从 drawRect 函数外部在当前上下文之上进行绘制?

UIView drawRect : is it possible to stroke inside a path?

objective-c - 核心 TextView 具有黑色背景色

iphone - 如何使注释副标题从标题菜单中消失?

iphone - View 的底部在模拟器和设备上被切断(单 View 应用程序)

ios - 如何为包括iPhone X在内的所有设备设置导航栏背景图片?

ios - 深色模式比浅色模式需要更多时间加载图片

ios - 运行序列时为 Action 动态分配参数

ios - 将目标添加到 Apple Watch 上 tableView 行上的按钮

ios - 线程 1 : EXC_BAD_INSTRUCTION Swift 2