保存CIImage,通过转换为CGImage,抛出错误 [英] Save CIImage, by converting to CGImage, throws an error

查看:31
本文介绍了保存CIImage,通过转换为CGImage,抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张从 AVFoundation 中抓取的图片:

I have an image I'm grabbing from AVFoundation:

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

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

然后我通过首先转换为 CIImage 来裁剪此图像:

I'm then cropping this image by first converting to a 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 以便我可以将其保存:

I'm then trying to convert this back to a CGImage so that I can save it out:

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 Domain=ALAssetsLibraryErrorDomain代码=-3304 无法为保存的照片编码图像."UserInfo=0x19fbd0 {NSUnderlyingError=0x18efa0 "无法编码图像保存的照片.", NSLocalizedDescription=无法编码图像保存的照片.}

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 的转换可能会导致问题.有什么想法吗?

I've read elsewhere that this can happen if the image was 0x0 pixels, and that the conversion from CIImage to CGImage can cause the problem. Any ideas?

谢谢.

推荐答案

我成功了!

我所做的是将所有代码替换为:

What I did is replace all my code with:

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.然后我可以用它来过滤乐趣,然后保存它:

This gives me a usable CIImage. I can then have filter fun with it, before saving it with:

CIImage *croppedColourImage = [crop outputImage];

//convert it to a cgimage for saving
CGImageRef cropColourImage = [context createCGImage:croppedColourImage fromRect:[croppedColourImage extent]];

//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);
    } else
    {
        NSLog(@"SUCCESS in image save");
        CGImageRelease(cropColourImage);
    }
}];

这篇关于保存CIImage,通过转换为CGImage,抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆