如何将使用AVFoundation拍摄的照片保存到相册?
AVFoundation对那些想要弄脏手的人来说非常棒,但是仍然有很多基本的东西不太容易想像如何将从设备拍摄的照片保存到相册
有任何想法吗?
以下是一步一步教程如何使用AVFoundation
捕获图像,并将其保存到相册。
添加一个UIView
对象到NIB(或作为子视图),并在你的控制器中创build一个@property
:
@property(nonatomic, retain) IBOutlet UIView *vImagePreview;
将UIView
连接到IB上面的sockets,或者如果使用代码而不是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]; }
创build一个新的@property
来保存对输出对象的引用:
@property(nonatomic, retain) AVCaptureStillImageOutput *stillImageOutput;
然后制作一个UIImageView
,我们将显示捕获的照片。 将其添加到您的NIB,或以编程方式。
挂钩到另一个@property
,或手动分配,例如;
@property(nonatomic, retain) IBOutlet UIImageView *vImage;
最后,创build一个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方法。 无论如何,这里有一些想法。 你可以很容易地在互联网上find例子。 祝你好运 :)
以这种方式设置摄像机有很多事情要做,要么你没有做或者没有显示。
AVCam示例项目中的AVCamCaptureManager.m
是最好的地方。 特别是setupSession
和setupSession
(将照片写入库)。
这个方法适用于我
-(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
实例。
- 状态为AVKeyValueStatusFailed时,重新加载AVAsset中的密钥
- iOS检测系统音量水平变化。 私人API或不? AVSystemController_SystemVolumeDidChangeNotification
- AVAssetWriter输出(UIImage写入video)失真
- IOS上的WaveForm
- 如何播放背景Swift中的audio?
- 核心图像 – 在CMSampleBufferRef上渲染一个透明的图像会导致它周围的黑框
- 有谁知道如何正确实施AVAssetResourceLoaderDelegate方法?
- 同时caching/播放AVPlayervideo
- 在forwardPlaybackEndTime之后恢复AVPlayer