I was trying to rotate an UIImage of 2448x3264 taken with the camera. When I do, the memory spikes 120Mb approximately, for more or less 3/4 seconds and then returns to its normal state. The problem is that, in devices with less memory(for instance, ipod touch), the app crashes. Even if it doesn't, I don't think it should use that much memory for one image. Iphone 5 is also lagged when this happens.
According to a comment in this answer, the size in bytes of the decompressed memory after using UIGraphicsGetCurrentContext() should be width * height * CGImageGetBitsPerComponent(image.CGImage) / 8 bytes, so the image should occupy 8Mb, not 120.
Any idea why this happens and how to fix it?
Here's the UIImage caterogy method to return a rotated image:
- (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;
}
Here's proof from instruments that the raster data is what's causing the peak in memory, just when the rotate method is executed:
答案 0 :(得分:0)
几条评论: