【发布时间】:2017-09-21 12:20:24
【问题描述】:
我试图旋转用相机拍摄的 2448x3264 的 UIImage。当我这样做时,内存大约达到 120Mb,大约持续 3/4 秒,然后恢复到正常状态。问题是,在内存较少的设备(例如,ipod touch)中,应用程序崩溃。即使没有,我认为它不应该为一张图像使用那么多内存。发生这种情况时,Iphone 5 也会滞后。
根据this答案中的评论,使用UIGraphicsGetCurrentContext()后解压内存的字节大小应该是width * height * CGImageGetBitsPerComponent(image.CGImage) / 8 bytes,所以图片应该占用8Mb,而不是120.
知道为什么会发生这种情况以及如何解决吗?
这是返回旋转图像的 UIImage 分类方法:
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees {
CGFloat radian = (CGFloat) (degrees * (M_PI/ 180.0f));
CGSize rotatedSize = [self rotatedImageSize:degrees];
// Create the bitmap context
UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, 0);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGPoint contextCenter = CGPointMake(rotatedSize.width/2.0f,
rotatedSize.height/2.0f);
CGContextTranslateCTM(bitmap, contextCenter.x, contextCenter.y);
// // Rotate the image context
CGContextRotateCTM(bitmap, radian);
// Now, draw the rotated/scaled image into the context
[self drawInRect:CGRectMake(-self.size.width/2.0f, -
self.size.height/2.0f, self.size.width, self.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
这是来自仪器的证明,即光栅数据是导致内存峰值的原因,就在执行旋转方法时:
【问题讨论】:
标签: ios objective-c uiimage core-graphics