NSInvalidArgument错误 - 来自CIImage的CGImageRef

mrE*_*pty 0 core-image nserror ios cgimageref

我在两天前运行正常的一些代码上收到错误.错误是:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType extent]: unrecognized selector sent to instance 0x1bc8f0'
Run Code Online (Sandbox Code Playgroud)

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

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
Run Code Online (Sandbox Code Playgroud)

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

greySave = [context createCGImage:greyscaleImage fromRect:[greyscaleImage extent]];
Run Code Online (Sandbox Code Playgroud)

greySave是一个CGImageRef,在我的视图控制器的开头声明.抓取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]];
Run Code Online (Sandbox Code Playgroud)

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

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);
                }
            }];
Run Code Online (Sandbox Code Playgroud)

任何想法为什么会这样?

谢谢.

编辑:

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

[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
                }
            }];
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

- 然后保存colourSave图像,这很好用.

- 然后方法结束.

还有一件事,就是我如何裁剪图像.从以下方法返回的图像是我从以下方法创建的CIImage:

-(UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
Run Code Online (Sandbox Code Playgroud)

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

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

return cropped;
Run Code Online (Sandbox Code Playgroud)

}

mrE*_*pty 8

添加答案,以防其他人帮助.

我正在将UIImage传递给CIFilter,这导致了问题.将UIImage转换为CIImage并传递它解决了问题.

  • 我多么喜欢StackOverflow.非常感谢空先生. (2认同)