ios - NSInvalidArgument 错误 - 来自 CIImage 的 CGImageRef

标签 ios core-image nserror cgimageref

我在两天前运行良好的某些代码中遇到错误。错误是:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType extent]: unrecognized selector sent to instance 0x1bc8f0'

它指向 main.m 上的这一行:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

使用日志查看我的代码到达的位置,似乎这一行引发了错误:

greySave = [context createCGImage:greyscaleImage fromRect:[greyscaleImage extent]];

greySave 是一个 CGImageRef,在我的 View Controller 的开头声明。获取 CIImage 输出并将其转换为 CGImageRef 以进行保存很简单:

desaturate = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, colourSave, @"inputIntensity", [NSNumber numberWithFloat:0.00], nil];


greyscaleImage = [desaturate valueForKey:@"outputImage"];


greySave = [context createCGImage:greyscaleImage fromRect:[greyscaleImage extent]];

我认为(虽然不明白为什么)可能导致问题的唯一改变是添加了一个额外的条件语句,如果用户希望这样做,它将应用第二个过滤器:

if ([_alphaButtonSavedState isEqualToString:@"ON"]) {
             NSLog(@"user does want alpha on greyscale");
            //user wants alpha mask also  
        } else {
             NSLog(@"user does not want alpha on greyscale");
            //convert to CGImage for saving
            greySave = [context createCGImage:greyscaleImage fromRect:[greyscaleImage extent]];
             NSLog(@"greySave cgimage created");

            //save the grey image
            [library writeImageToSavedPhotosAlbum:greySave metadata:[greyscaleImage properties] completionBlock:^(NSURL *assetURL, NSError *error){
                if (error) {
                    NSLog(@"ERROR in greyscale save: %@", error);
                } else
                {
                    NSLog(@"SUCCESS in greyscale save");
                    //in here we'll put a nice animation or something
                    //CGImageRelease(greyscaleImage);
                }
            }];

知道为什么会这样吗?

谢谢。

编辑:

这是整个代码块,以防万一。

[stillImage captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

    NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];  //get the raw image data from the session
    UIImage *startImage = [[UIImage alloc] initWithData:imageData];  //create a UIImage from that image data, let's us work with it

    //resizing of image to take place before anything else
    UIImage *image = [UIImage imageWithImage:startImage scaledToSize:_exportSSize];  //scale the image so it's shortest side is the size given in prefs

    NSLog(@"image width post scale: %g", image.size.width);
    NSLog(@"image height post scale is: %g", image.size.height);

    //change the context to render using cpu, so on app exit renders get completed
    context = [CIContext contextWithOptions: [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:kCIContextUseSoftwareRenderer]];

    //new crop testing - not using CIImage - seems to work, just need to sort out crop region
    CGRect cropRect = CGRectMake(0, 0, _exportSize, _exportSize);
    UIImage *cropper = [self imageByCropping:image toRect:cropRect];

    //the colour image is always saved so create a CGImage to save later
    colourSave = [cropper CGImage];
    NSLog(@"coloursave image created");

    //now we convert and create CGImages based upon chosen export options
    if ([_greyButtonSavedState isEqualToString:@"ON"]) {
        NSLog(@"inside the greyscale conditional");
        //user wants greyscale image
        //create a CIMonochrome filter 
        desaturate = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, colourSave, @"inputIntensity", [NSNumber numberWithFloat:0.00], nil];
         NSLog(@"desat ci filter created");
        //get the output
        greyscaleImage = [desaturate valueForKey:@"outputImage"];
         NSLog(@"dest output image created");

        //if the user wants to add the alpha
        if ([_alphaButtonSavedState isEqualToString:@"ON"]) {
             NSLog(@"user does want alpha on greyscale");
            //user wants alpha mask also  
        } else {
             NSLog(@"user does not want alpha on greyscale");
            //convert to CGImage for saving
            greySave = [context createCGImage:greyscaleImage fromRect:[greyscaleImage extent]];
             NSLog(@"greySave cgimage created");

            //save the grey image
            [library writeImageToSavedPhotosAlbum:greySave metadata:[greyscaleImage properties] completionBlock:^(NSURL *assetURL, NSError *error){
                if (error) {
                    NSLog(@"ERROR in greyscale save: %@", error);
                } else
                {
                    NSLog(@"SUCCESS in greyscale save");
                    //in here we'll put a nice animation or something
                }
            }];
        }
    }

-- 这里还有两个条件,目前都是空的

-- 随后保存 colourSave 图像,效果很好。

-- 然后方法结束。

还有一件事,这就是我裁剪图像的方式。以下方法返回的图像是我创建 CIImage 的来源:

-(UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect

{ CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);

UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);

return cropped;

最佳答案

添加答案以防对其他人有帮助。

我正在将 UIImage 传递到 CIFilter,这是导致问题的原因。将 UIImage 转换为 CIImage 并将其传入解决了该问题。

关于ios - NSInvalidArgument 错误 - 来自 CIImage 的 CGImageRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10564853/

相关文章:

android - Admob 广告无法加载状态详细信息 有限的广告服务

ios - 如何让用户在保存时自定义文件名?

macos - 哪些字段 -[NSResponder PresentError :] show?

.net - MonoTouch : UIImage. asJPEG 需要一个 NSError 对象

ios - CKError localizedDescription

ios - Parse.com 查询和应用程序设计问题 : A long-running operation is being executed on the main thread

ios - 如何从子项目中包含一个类

ios - 如何使用 CoreImage 创建平滑的阈值过滤器?

ios - 使用 Core Image 自定义滤镜在两个图像之间实现颜色传输

ios - 带有 CIQRCodeGenerator 的 CIFilter 导致崩溃