2012-05-04 39 views
0

我有一个形象,我是从AVFoundation抓:保存CIImage,通过转换为CGImage,抛出一个错误

[stillImage captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 
    NSLog(@"image capture"); 

    NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
    UIImage *image = [[UIImage alloc] initWithData:imageData]; 

我那么首先转换为CIImage剪裁此图片:

beginImage = [CIImage imageWithCVPixelBuffer:CMSampleBufferGetImageBuffer(imageSampleBuffer) options:[NSDictionary dictionaryWithObjectsAndKeys:[NSNull null], kCIImageColorSpace, nil]]; 

    //first we crop to a square 
    crop = [CIFilter filterWithName:@"CICrop"]; 
    [crop setValue:[CIVector vectorWithX:0 Y:0 Z:70 W:70] forKey:@"inputRectangle"]; 
    [crop setValue:beginImage forKey:@"inputImage"]; 
    croppedColourImage = [crop valueForKey:@"outputImage"]; 

我再尝试转换这回CGImage,这样我可以节省出来:

CGImageRef cropColourImage = [context createCGImage:croppedColourImage fromRect:[croppedColourImage extent]]; 
    UIImage *cropSaveImage = [UIImage imageWithCGImage:cropColourImage]; 


    //saving of the images 
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

    [library writeImageToSavedPhotosAlbum:[cropSaveImage CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){ 
    if (error) { 
     NSLog(@"ERROR in image save :%@", error); 
    } else 
    { 
     NSLog(@"SUCCESS in image save"); 
    } 

    }]; 

但是,这将引发错误:

ERROR in image save :Error Domain=ALAssetsLibraryErrorDomain Code=-3304 "Failed to encode image for saved photos." UserInfo=0x19fbd0 {NSUnderlyingError=0x18efa0 "Failed to encode image for saved photos.", NSLocalizedDescription=Failed to encode image for saved photos.}

我读过其他地方,这可能发生,如果图像是为0x0像素,以及从CIImage到CGImage转换可能会导致问题。有任何想法吗?

谢谢。

+0

我相信这是因为我试图保存的图像没有尺寸,宽度和高度显示为空。 “2012-05-06 14:39:22.886 zApp [5108:707]正在保存的图像的宽度为:(null) 2012-05-06 14:39:22.890 zApp [5108:707]正在保存的图像的高度为:(null)“ – mrEmpty

+0

我在我的NSLog代码中有一个错误,我可以确认我想要保存的图像是0x0像素。所以我认为是问题所在,但我不明白会发生什么。 – mrEmpty

回答

1

我得到了它的工作(战地3上的20分钟让我有时间思考 - 这让我想起任何PS3 BF3玩家想要给我发送消息)。

我所做的是取代我的所有太妃糖代码:

NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; 
UIImage *image = [[UIImage alloc] initWithData:imageData]; 
NSLog(@"*image size is: %@", NSStringFromCGSize(image.size)); 

//test creating a new ciimage 
CIImage *ciImage = [[CIImage alloc] initWithCGImage:[image CGImage]]; 
NSLog(@"ciImage extent: %@", NSStringFromCGRect([ciImage extent])); 

这给了我一个可用的CIImage。那么我可以过滤它的乐趣,与保存前:

CIImage *croppedColourImage = [crop outputImage]; 
    //convert it to a cgimage for saving 
    CGImageRef cropColourImage = [context createCGImage:croppedColourImage fromRect:[croppedColourImage extent]]; 
    ////NSLog(@"beginImage width is: %@", NSStringFromCGRect([beginImage extent])); 
    ////NSLog(@"croppedColourImage extent: %@", NSStringFromCGRect([croppedColourImage extent])); 
    ////NSLog(@"cropColourImage size: %zu", CGImageGetWidth(cropColourImage)); 
    //UIImage *cropSaveImage = [UIImage imageWithCGImage:cropColourImage]; 


    //saving of the images 
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 

    [library writeImageToSavedPhotosAlbum:cropColourImage metadata:[croppedColourImage properties] completionBlock:^(NSURL *assetURL, NSError *error){ 
    if (error) { 
     NSLog(@"ERROR in image save: %@", error); 
     //NSLog(@"Image being saved has width of: %zu", CGImageGetWidth(cropColourImage)); 
     // NSLog(@"Image being saved has height of: %zu", CGImageGetHeight(cropColourImage)); 
    } else 
    { 
     NSLog(@"SUCCESS in image save"); 
     CGImageRelease(cropColourImage); 
    } 

    }]; 

请忽略废话很多注释行,这些都是我的测试。他们现在可以摆脱。

我很高兴周围的花园接近庆祝运行。

+0

为什么cropColourImage没有发布出错? –