gpt4 book ai didi

ios - 保存CIImage,通过转换为CGImage,抛出错误

转载 作者:行者123 更新时间:2023-11-29 04:40:37 29 4
gpt4 key购买 nike

我有一张从 AVFoundation 获取的图像:

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

谢谢。

最佳答案

我成功了!

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

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]];

//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);
}
}];

关于ios - 保存CIImage,通过转换为CGImage,抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10447297/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com