2012-10-28 122 views

回答

62

这里是一步一步的教程,如何使用AVFoundation捕捉图像并将其保存到相册。

添加UIView对象笔尖(或作为一个子视图),并创建你的控制器@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

+2

正是我所需要的 – RollRoll

+1

我会强烈建议其他人访问这篇文章中链接的网站。他们对正在发生的事情有更深入的了解。好帖子,iDev! – bvd

+0

这是一个无价的答案,谢谢iDev! – Fattie

0

以这种方式设置相机的方法很多,您可能没有做或没有显示。

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

1

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

如果您还没有捕获静止图像,可以查看AVFoundation框架的captureStillImageAsynchronouslyFromConnection方法。无论如何,这里有一些想法。你可以很容易地在互联网上找到例子。 祝你好运:)

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实例。

+0

谢谢,但是您是如何使用AVFoundation从前置摄像头设备提取UIImage的? – RollRoll