如何将使用AVFoundation拍摄的照片保存到相册?

Rol*_*oll 12 iphone cocoa objective-c avfoundation ios

AVFoundation对于那些想要弄脏手的人来说非常棒,但仍然有很多基本的东西不容易想象如何将从设备拍摄的照片保存到相册

有任何想法吗?

iDe*_*Dev 62

以下是有关如何使用图像捕获图像AVFoundation并将其保存到相册的分步教程.

UIView对象添加到NIB(或作为子视图),并@property在控制器中创建一个:

@property(nonatomic, retain) IBOutlet UIView *vImagePreview;
Run Code Online (Sandbox Code Playgroud)

UIView如果您使用的是代码而不是NIB,请将其连接到IB上方的插座,或直接分配.

然后编辑你的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];
}
Run Code Online (Sandbox Code Playgroud)

创建一个新的@property来保存对输出对象的引用:

@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
Run Code Online (Sandbox Code Playgroud)

然后UIImageView在我们将显示捕获的照片的位置.将其添加到您的NIB或以编程方式.

将其连接到另一个@property,或手动分配,例如;

@property(nonatomic, retain) IBOutlet UIImageView *vImage;
Run Code Online (Sandbox Code Playgroud)

最后,创建一个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);
     }];
}
Run Code Online (Sandbox Code Playgroud)

您可能还需要导入#import <ImageIO/CGImageProperties.h>.

来源.还检查一下.

  • 正是我需要的 (2认同)
  • 我强烈建议其他人访问这篇文章中链接的网站.他们更深入地了解正在发生的事情.伟大的帖子,iDev! (2认同)