裁剪图像后处理像素时崩溃 [英] Crash when process pixel after crop an image

查看:496
本文介绍了裁剪图像后处理像素时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此功能裁剪图像后:

After using this function to crop image:

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

我将结果用于像素那样的过程:

I bring "result" to use for process with pixel like that:

......

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);

我会崩溃吗?有人知道,请帮帮我。

I will have crash? Anyone know about it, please help me.

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

update crash report: : copy_read_only: vm_copy failed: status 1.

推荐答案

经过很长一段时间才找到这次崩溃的解决方案。我有一个经验:
- 在iOS上处理位图时:如果我们没有注意到图像的方向,也许我们会遇到一些奇怪的崩溃。

after a long time find solution for this crash. I got an experience: - When process bitmap on iOS: if we don't notice orientation of image, maybe we got some strange crash.

用我的崩溃上面:我已经修复了很简单:更改

With my crash above: I have fixed it very simple: change

UIImage *result = [UIImage imageWithCGImage:imageRef];

by

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

这篇关于裁剪图像后处理像素时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆