0

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 への変換が問題を引き起こす可能性があることを他の場所で読みました。何か案は?

ありがとうございました。

4

1 に答える 1

1

うまくいきました!

私がしたことは、すべてのコードを次のものに置き換えることです。

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);
    }
}];
于 2012-05-06T17:59:58.110 に答える