我正在使用以下代码来旋转图像
http://www.platinumball.net/blog/2010/01/31/iphone-uiimage-rotation-and-scaling/
这是我在将图像上传到服务器之前所做的为数不多的图像转换之一,我还有一些其他转换:规范化、裁剪、调整大小。
每个转换都会返回一个 (UIImage*)
并且我使用类别添加这些函数。我这样使用它:
UIImage *img = //image from camera;
img = [[[img normalize] rotate] scale] resize];
[upload img];
每次从相机中选择 3~4 张照片并执行相同的代码后,我在 XCode 中收到内存警告消息。
我猜我某处有内存泄漏(即使我使用的是 ARC)。我对 xCode 调试工具的使用不是很有经验,所以我开始在每个方法之后打印保留计数。
UIImage *img = //image from camera;
img = [img normalize];
img = [img rotate]; // retain count increases :(
img = [img scale];
img = [img resize];
增加保留计数的唯一操作是旋转。这正常吗?
最佳答案
The only operation that increases the retain count is the rotation. Is this normal?
rotate
函数中的 UIGraphicsGetImageFromCurrentImageContext()
调用很可能最终会保留图像。如果是这样,它几乎肯定也会按照正常的 Cocoa 内存管理规则自动释放图像。无论哪种方式,你都不应该担心。只要您的 rotate
函数本身不包含任何不平衡的 retain
(或 alloc
、new
或 copy
) 调用,你应该期望没有泄漏。如果您确实怀疑有泄漏,最好使用 Instruments 追踪它,而不是自己观察 retainCount
。
关于ios - 图像旋转后保留计数应该增加吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9775254/