iphone - 裁剪图像后处理像素时崩溃

标签 iphone ios objective-c image-processing

使用此功能裁剪图片后:

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。

最佳答案

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

上面的崩溃:我很简单地修复了它:更改

UIImage *result = [UIImage imageWithCGImage:imageRef];

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

关于iphone - 裁剪图像后处理像素时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17690401/

相关文章:

iphone - 如何将变量参数传递给另一个方法?

ios - CGFloat 最小值与当前值

ios - 更改所选 TextField 的背景颜色

ios - swift 3 中的 NSNumber 问题

iOS Youtube 直播 API 错误

Objective-C播放声音

iTunes Connect 中的 Iphone 应用程序状态

iphone - 如何为不同平台(iPhone 和 Mac OS X)创建具有多个目标的 xcode 项目?

iphone - 为 UI 导航栏设置提示

ios - Swift GCD 中的持久线程