gpt4 book ai didi

ios - CG 光栅数据对于一张图像来说太大

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:11:03 26 4
gpt4 key购买 nike

我试图旋转用相机拍摄的 2448x3264 的 UIImage。当我这样做时,内存大约会达到 120Mb 的峰值,大约持续 3/4 秒,然后恢复到正常状态。问题在于,在内存较少的设备(例如 ipod touch)中,应用程序会崩溃。即使没有,我也不认为它应该为一张图像使用那么多内存。发生这种情况时,Iphone 5 也会卡顿。

根据 this 中的评论回答,使用UIGraphicsGetCurrentContext()后解压后的内存字节大小应该是width * height * CGImageGetBitsPerComponent(image.CGImage)/8 bytes,所以图片应该占8Mb,而不是120。

知道为什么会发生这种情况以及如何解决它吗?

这是返回旋转图像的 UIImage caterogy 方法:

- (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;
}

这是来自仪器的证据,表明栅格数据是导致内存峰值的原因,就在执行旋转方法时:

Instruments proof

最佳答案

几点说明:

  • 2448x3264x(比如说 4 字节/像素)大约是 30 兆字节。
  • 不要旋转它。如果它出现在屏幕上,您可以使用 View 的转换属性。如果它进入一个文件并且旋转是 8 个 exif 方向之一,请写入说明哪个方向向上的元数据。同样,如果它来自文件,请使用图像 I/O 并要求 CGImageSourceRef 生成一个定向缩略图,其大小需要将其显示在屏幕上。 (提示使用 kCGImageSourceThumbnailMaxPixelSize 和 kCGImageSourceCreateThumbnailWithTransform。)对于任意角度,您可以使用 CoreImage 过滤器来旋转图像并缩小采样到屏幕所需的大小(在 GPU 上)。同样,您可以使用 vImage 在 CPU 上执行相同的操作(提示 vImageAffineWarpCG_ARGB8888)。 vImage 非常快,它会给你比核心图形更好的重采样。等等......如果它伤害了,就不要这样做。
  • 在花时间将角度转换为旋转矩阵之前,您应该暂停一下。例如,如果你的角度是 90 度,你想要的矩阵就是 [0 1 -1 0 0 0] 并且你的重采样代码会比你计算的近似结果更快乐。旋转矩阵是 [cos(t) sin(t) -sin(t) cos(t) 0 0]。客户可能已经可以直接传入余弦和正弦。

关于ios - CG 光栅数据对于一张图像来说太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43594513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com