[英]How to save photos taken using AVFoundation to Photo Album?
AVFoundation对于那些想要弄脏手的人来说非常棒,但仍然有很多基本的东西不容易想象如何将从设备拍摄的照片保存到相册
有任何想法吗?
这是一个有关如何使用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>
。
根据你的问题,看起来你已经从相机到NSData或UIImage的照片。 如果是这样,您可以以不同的方式将此图片添加到相册。 AVFoundation本身没有承载图像的类。 因此,例如,您可以使用ALAssetsLibrary框架将图像保存到相册。 或者你可以使用UIKit框架和它的UIImageWriteToSavedPhotosAlbum方法。 两者都很好用。
如果您还没有捕获静态图像,可以查看AVFoundation框架的captureStillImageAsynchronouslyFromConnection方法。 无论如何,这是想法。 您可以轻松地在Internet上找到示例。 祝好运 :)
以这种方式设置相机有很多,你要么没有做,要么没有显示。
最好看的地方是:AVCam示例项目中的AVCamCaptureManager.m
; 特别是setupSession
和captureStillImage
(将照片写入库)。
这种方法适合我
-(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");
}
}];
}
其中ALAssetLibrary
是ALAssetLibrary
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.