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");
}
}];
ただし、これによりエラーがスローされます。
画像保存エラー: エラー ドメイン=ALAssetsLibraryErrorDomain コード=-3304 「保存した写真の画像をエンコードできませんでした。」UserInfo=0x19fbd0 {NSUnderlyingError=0x18efa0 "保存した写真の画像をエンコードできませんでした。", NSLocalizedDescription=保存した写真の画像をエンコードできませんでした。}
画像が 0x0 ピクセルの場合にこれが発生する可能性があり、CIImage から CGImage への変換が問題を引き起こす可能性があることを他の場所で読みました。何か案は?
ありがとうございました。