12

AVFoundation 非常适合那些想弄脏手的人,但是还有很多基本的东西不容易弄清楚,比如如何将设备拍摄的照片保存到相册

有任何想法吗?

4

4 回答 4

61

Here is a step by step tutorial on how to capture an image using AVFoundation and save it to photo album.

Add a UIView object to the NIB (or as a subview), and create a @property in your controller:

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

Connect the UIView to the outlet above in IB, or assign it directly if you’re using code instead of a NIB.

Then edit your UIViewController, and give it the following viewDidAppear method:

-(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];
}

Create a new @property to hold a reference to output object:

@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;

Then make a UIImageView where we’ll display the captured photo. Add this to your NIB, or programmatically.

Hook it up to another @property, or assign it manually, e.g.;

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

Finally, create a UIButton, so that you can take the photo.

Again, add it to your NIB (or programmatically to your screen), and hook it up to the following method:

-(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);
     }];
}

You might have to import #import <ImageIO/CGImageProperties.h> also.

Source. Also check this.

于 2012-11-03T06:43:33.820 回答
1

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

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

于 2012-11-02T14:40:18.997 回答
0

以这种方式设置相机有很多你没有做或没有展示的地方。

最好看的地方是:AVCamCaptureManager.m在 AVCam 示例项目中;特别是setupSessioncaptureStillImage(将照片写入图书馆)。

于 2012-10-28T05:24:26.200 回答
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 回答