在裁剪图像后处理像素时发生崩溃

使用此function裁剪图像后:

CGImageRef imageRef = CGImageCreateWithImageInRect([self.imageInput CGImage], rect); UIImage *result = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); 

我带“结果”用于像素那样的处理:

 ...... CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); CGContextRelease(context); // Now your rawData contains the image data in the RGBA8888 pixel format. int byteIndex = 0; for (NSInteger i = 0 ; i < width * height; i++) { int R = rawData[byteIndex]; int G = rawData[byteIndex + 1]; int B = rawData[byteIndex + 2]; int test = [self getValueof:0.3* R + 0.59* G + 0.11 * B inRange:0 to:255]; rawData[byteIndex] = (char)(test); rawData[byteIndex + 1] = (char)(test); rawData[byteIndex + 2] = (char)(test); byteIndex += 4; } ctx = CGBitmapContextCreate(rawData, CGImageGetWidth( imageRef ), CGImageGetHeight( imageRef ), 8, CGImageGetBytesPerRow( imageRef ), CGImageGetColorSpace( imageRef ), kCGImageAlphaPremultipliedLast ); imageRef = CGBitmapContextCreateImage (ctx); UIImage* rawImage = [UIImage imageWithCGImage:imageRef]; CGContextRelease(ctx); CGImageRelease(imageRef); free(rawData); 

我会崩溃? 任何人都知道,请帮助我。

更新崩溃报告::copy_read_only:vm_copy失败:状态1。

经过很长时间才find这个崩溃的解决scheme。 我有一个经验: – 当在iOS上处理位图:如果我们没有注意到图像的方向,也许我们有一些奇怪的崩溃。

以上我的崩溃:我已经修复它非常简单:更改

 UIImage *result = [UIImage imageWithCGImage:imageRef]; 

通过

 UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.imageInput.scale orientation:self.imageInput.imageOrientation];