我在兩天前運行正常的一些代碼上出現錯誤。錯誤是:NSInvalidArgument錯誤 - 來自CIImage的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,在我的視圖控制器的開始聲明。這是簡單的抓住一個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;
}
如果有幫助,CIFilter的輸入圖像是UIImage。 – mrEmpty
您可以在創建'CGImage'的行上放置一個斷點並查看'greyscaleImage'設置爲什麼?錯誤是說對象'greyscaleImage'沒有實現'-extent'方法。 'greyscaleImage'可以是零或其他類型的對象嗎? – user1118321
@ user1118321在邏輯上,greyscaleImage是一個CGImage,因爲它從NSCFType繼承。它不是零,因爲那樣它就不會崩潰,因爲消息不會產生零。 – CodaFi