我有一個「ImageManipulator」類,用於在iPhone上執行一些裁剪,調整大小和旋轉相機圖像。在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大小,我仍然看到了尖峯我以後想知道是否有是一種不同的/更有效的方式來完成上述的旋轉?
在此先感謝您的回覆。 Rog
圖像有多大?我們有類似的問題,所以我們首先將圖像縮放到可接受的尺寸,然後應用旋轉。 –
您是否在冗長的操作中創建大型自動釋放對象? – Eiko
內存尖峯是因爲'CGBitmapContextCreate'必須分配一個緩衝區來保存圖像數據,這將是關於'width * height * CGImageGetBitsPerComponent(self.CGImage)/ 8'字節。如果需要在繪製原始圖像之前加載或解壓縮原始圖像的圖像數據,'CGContextDrawImage'也可能分配另一個相似大小的緩衝區。不過,我不知道更有效的方法。 – Anomie