如何保存使用AVFoundation到相册拍摄的照片? [英] How to save photos taken using AVFoundation to Photo Album?
问题描述
AVFoundation只是为那些谁想要弄脏手,但仍然有很多基本的东西不容易想像如何保存从设备拍摄的照片相册
AVFoundation is just great for those who want to get hands dirty, but there still a lot of basic stuff not easy to figure like how to save the pictures taken from device to Photo Album
任何想法?
推荐答案
这里是一个一步一步教程如何使用 AVFoundation
并将其保存到相册。
Here is a step by step tutorial on how to capture an image using AVFoundation
and save it to photo album.
添加 UIView
对象(或作为子视图),并在您的控制器中创建 @property
:
Add a UIView
object to the NIB (or as a subview), and create a @property
in your controller:
@property(nonatomic, retain) IBOutlet UIView *vImagePreview;
将 UIView
连接到
然后编辑您的 UIViewController
,如果您使用的是代码,并给它以下 viewDidAppear
方法:
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];
}
创建一个新的 @property
保存对输出对象的引用:
Create a new @property
to hold a reference to output object:
@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
然后制作 UIImageView
显示捕获的照片。
Then make a UIImageView
where we’ll display the captured photo. Add this to your NIB, or programmatically.
将它添加到另一个 @property
,或手动分配,
Hook it up to another @property
, or assign it manually, e.g.;
@property(nonatomic, retain) IBOutlet UIImageView *vImage;
最后,创建一个 UIButton
再次,将它添加到您的NIB(或以编程方式添加到您的屏幕),并挂钩到以下方法:
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);
}];
}
您可能需要导入 #import< ImageIO /CGImageProperties.h>
You might have to import #import <ImageIO/CGImageProperties.h>
also.
来源。此外,请检查此。
这篇关于如何保存使用AVFoundation到相册拍摄的照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!