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

时间:2012-10-28 04:59:42

标签: iphone objective-c ios cocoa avfoundation

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

有什么想法吗?

4 个答案:

答案 0 :(得分:62)

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

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

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

UIView连接到IB上方的插座,或者如果您使用的是代码而不是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>

Source。另请查看this

答案 1 :(得分:1)

根据您的问题,看起来您已经将相机中的照片转换为NSData或UIImage。如果是这样,您可以以不同的方式将此图片添加到相册。 AVFoundation本身没有承载图像的类。 因此,例如,您可以使用ALAssetsLibrary框架将图像保存到相册。 或者你可以使用它的UIImageWriteToSavedPhotosAlbum方法使用UIKit框架。两者都很好用。

如果您还没有捕获静态图像,可以查看AVFoundation框架的captureStillImageAsynchronouslyFromConnection方法。 无论如何,这是想法。您可以轻松地在Internet上找到示例。 祝你好运:)

答案 2 :(得分:0)

以这种方式设置相机有很多功能,要么你没有做或没有显示。

最佳观看位置是:AVCam示例项目中的AVCamCaptureManager.m;特别是setupSessioncaptureStillImage(将照片写入图书馆)。

答案 3 :(得分: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实例。