12

AVFoundationは手を汚したい人には最適ですが、デバイスから撮影した写真をフォトアルバムに保存する方法など、理解しにくい基本的なものがまだたくさんあります。

何か案は?

4

4 に答える 4

61

AVFoundationこれは、を使用して画像をキャプチャし、フォトアルバムに保存する方法のステップバイステップのチュートリアルです。

UIViewオブジェクトをNIBに(またはサブビューとして)追加@propertyし、コントローラーにを作成します。

@property(nonatomic, retain) IBOutlet UIView *vImagePreview;

を上記のIBのコンセントに接続するUIViewか、NIBの代わりにコードを使用している場合は直接割り当てます。

次に、を編集してUIViewController、次のviewDidAppear方法を指定します。

-(void)viewDidAppear:(BOOL)animated
{
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetMedium;

    CALayer *viewLayer = self.vImagePreview.layer;
    NSLog(@"viewLayer = %@", viewLayer);

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
    [self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // Handle the error appropriately.
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];

    stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [stillImageOutput setOutputSettings:outputSettings];
    [session addOutput:stillImageOutput];

    [session startRunning];
}

@property出力オブジェクトへの参照を保持するために新しいを作成します。

@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;

次にUIImageView、キャプチャした写真を表示する場所を作成します。これをNIBに追加するか、プログラムで追加します。

別のものに接続する@propertyか、手動で割り当てます。例:

@property(nonatomic, retain) IBOutlet UIImageView *vImage;

最後にUIButton、写真を撮れるようにを作成します。

繰り返しますが、それをNIB(またはプログラムで画面に)に追加し、次の方法に接続します。

-(IBAction)captureNow {
    AVCaptureConnection *videoConnection = nil;
    for (AVCaptureConnection *connection in stillImageOutput.connections)
    {
        for (AVCaptureInputPort *port in [connection inputPorts])
        {
            if ([[port mediaType] isEqual:AVMediaTypeVideo] )
            {
                videoConnection = connection;
                break;
            }
        }
        if (videoConnection) 
            { 
                break; 
            }
    }

    NSLog(@"about to request a capture from: %@", stillImageOutput);
    [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
    {
         CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
         if (exifAttachments)
         {
            // Do something with the attachments.
            NSLog(@"attachements: %@", exifAttachments);
         } else {
            NSLog(@"no attachments");
             }

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

        self.vImage.image = image;

        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
     }];
}

インポート#import <ImageIO/CGImageProperties.h>も必要になる場合があります。

ソースこれもチェックしてください。

于 2012-11-03T06:43:33.820 に答える
1

あなたの質問によると、あなたはすでにカメラからNSDataまたはUIImageに写真を持っているようです。その場合は、さまざまな方法でこの写真をアルバムに追加できます。AVFoundation自体には、画像の保持を実行するクラスはありません。したがって、たとえば、ALAssetsLibraryフレームワークを使用して、画像をフォトアルバムに保存できます。または、UIKitフレームワークとそのUIImageWriteToSavedPhotosAlbumメソッドだけを使用することもできます。どちらも使いやすいです。

まだ画像をキャプチャしていない場合は、AVFoundationフレームワークのcaptureStillImageAsynchronouslyFromConnectionメソッドを確認できます。とにかく、ここにアイデアがあります。インターネットで簡単に例を見つけることができます。幸運を :)

于 2012-11-02T14:40:18.997 に答える
0

この方法は私のために働きます

-(void) saveImageDataToLibrary:(UIImage *) image
{
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[appDelegate.library writeImageDataToSavedPhotosAlbum:imageData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error)
{
    if (error) {
        NSLog(@"%@",error.description);
    }
    else {
        NSLog(@"Photo saved successful");
    }
}];
}

ここで、appDelegate.libraryはALAssetLibraryインスタンスです。

于 2012-11-02T14:55:01.673 に答える
0

このようにカメラを設定することには、実行していないか、表示していないことがたくさんあります。

見るのに最適な場所AVCamCaptureManager.mは次のとおりです。AVCamサンプルプロジェクト。特にsetupSessionand captureStillImage(写真をライブラリに書き込みます)。

于 2012-10-28T05:24:26.200 に答える