iphone – 如何将使用AVFoundation拍摄的照片保存到相册?

iphone – 如何将使用AVFoundation拍摄的照片保存到相册?,第1张

概述AVFoundation对于那些想要弄脏手的人来说是非常棒的,但是仍然有很多基本的东西不容易,如何将从设备拍摄照片保存到相册 有任何想法吗? 这是一个关于如何使用AVFoundation捕获图像并将其保存到相册的分步教程. 将UIView对象添加到NIB(或作为子视图),并在控制器中创建一个@property: @property(nonatomic, retain) IBOutlet UIVi AVFoundation对于那些想要弄脏手的人来说是非常棒的,但是仍然有很多基本的东西不容易,如何将从设备拍摄的照片保存到相册

有任何想法吗?

解决方法 这是一个关于如何使用AVFoundation捕获图像并将其保存到相册的分步教程.

将UIVIEw对象添加到NIB(或作为子视图),并在控制器中创建一个@property:

@H_502_21@@property(nonatomic,retain) IBOutlet UIVIEw *vImagePrevIEw;

将UIVIEw连接到IB上方的插座,或者如果您使用代码而不是NIB,则可以直接分配.

然后编辑你的UIVIEwController,并给它以下vIEwDIDAppear方法:

@H_502_21@-(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来保存对输出对象的引用:

@H_502_21@@property(nonatomic,retain) AVCaptureStillimageOutput *stillimageOutput;

然后制作一个UIImageVIEw,我们将显示捕获的照片.将其添加到您的NIB,或以编程方式.

挂钩到另一个@property,或手动分配,例如

@H_502_21@@property(nonatomic,retain) IBOutlet UIImageVIEw *vImage;

最后,创建一个UIbutton,以便您可以拍照.

再次将其添加到您的NIB(或以编程方式添加到您的屏幕),并将其连接到以下方法:

@H_502_21@-(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); }];}

您可能需要导入#import< ImageIO / CGImagePropertIEs.h>也.

Source.另请查看this.

总结

以上是内存溢出为你收集整理的iphone – 如何将使用AVFoundation拍摄的照片保存到相册?全部内容,希望文章能够帮你解决iphone – 如何将使用AVFoundation拍摄的照片保存到相册?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1025660.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存