Iphone 如何将使用AVFoundation拍摄的照片保存到相册?
AVFoundation非常适合那些想弄脏手的人,但还有很多基本的东西不容易弄清楚,比如如何将从设备拍摄的照片保存到相册Iphone 如何将使用AVFoundation拍摄的照片保存到相册?,iphone,objective-c,ios,cocoa,avfoundation,Iphone,Objective C,Ios,Cocoa,Avfoundation,AVFoundation非常适合那些想弄脏手的人,但还有很多基本的东西不容易弄清楚,比如如何将从设备拍摄的照片保存到相册 有什么想法吗?以这种方式设置相机有很多方法,你要么不做,要么不显示 最好看的地方是:在AVCam样本项目中;特别是setupSession和captureshillimage(将照片写入库)。根据您的问题,看起来您已经将相机中的图片导入了NSData或UIImage。如果是这样,您可以用不同的方式将此图片添加到相册中。AVFoundation本身没有携带图像的类。 例如,您可
有什么想法吗?以这种方式设置相机有很多方法,你要么不做,要么不显示
最好看的地方是:在AVCam样本项目中;特别是
setupSession
和captureshillimage
(将照片写入库)。根据您的问题,看起来您已经将相机中的图片导入了NSData或UIImage。如果是这样,您可以用不同的方式将此图片添加到相册中。AVFoundation本身没有携带图像的类。
例如,您可以使用该框架将图像保存到相册中。
或者您可以将UIKit框架与它的方法一起使用。两者都很好用
如果您还没有捕获到静止图像,您可以查看AVFoundation框架的方法。
无论如何,这里有一些想法。你可以很容易地在网上找到例子。
祝你好运:)这个方法适合我
-(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的
实例。这里有一个关于如何使用AVFoundation
捕获图像并将其保存到相册的分步教程
将ui视图
对象添加到NIB(或作为子视图),并在控制器中创建@属性
:
@property(nonatomic, retain) IBOutlet UIView *vImagePreview;
将ui视图
连接到IB中上面的插座,或者如果您使用的是代码而不是NIB,则直接分配它
然后编辑您的UIViewController
,并为其提供以下viewdideappear
方法:
-(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(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
然后制作一个UIImageView
,我们将在其中显示捕获的照片。将其添加到NIB,或以编程方式添加
将其连接到另一个@属性
,或手动分配,例如
@property(nonatomic, retain) IBOutlet UIImageView *vImage;
最后,创建一个ui按钮
,以便拍照
同样,将其添加到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);
}];
}
您可能还需要导入#导入
。另请检查。谢谢,但是您是如何使用AVFoundation从前置摄像头设备提取UIImage的?我强烈建议其他人访问本文中链接的网站。他们对正在发生的事情有了更深入的了解。棒极了,艾德夫!工作得很好。也许也应该叫[super ViewDidDisplay:animated],非常感谢。我有它的Swift版本代码。任何需要的人,请告诉我。这个话题似乎不涉及可可。