我有一个从相机拉出来的预览图层,它应该是工作的.当我按一个按钮时,我想要拍照.我已经启动了AVCaptureStill
ImageOutput,如下所示:
AVCaptureStillImageOutput *avCaptureImg = [[AVCaptureStillImageOutput alloc] init];
然后我试图用这个对象拍照:
[avCaptureImg captureStillImageAsynchronouslyFromConnection:(AVCaptureConnection *) completionHandler:^(CMSampleBufferRef imageDataSampleBuffer,NSError *error) { }];
我需要帮助如何拍照并将其保存在变量中.谢谢
解决方法
您需要确定定义一个AVCaptureVideoPreviewLayer&将其添加到视图层:
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; [self.view.layer addSublayer:captureVideoPreviewLayer];
这将连接到您的AVCaptureDeviceInput
这是完整的解决方案:
///////////////////////////////////////////////// //// //// Utility to find front camera //// ///////////////////////////////////////////////// -(AVCaptureDevice *) frontFacingCameraifAvailable{ NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; AVCaptureDevice *captureDevice = nil; for (AVCaptureDevice *device in videoDevices){ if (device.position == AVCaptureDevicePositionFront){ captureDevice = device; break; } } // Couldn't find one on the front,so just get the default video device. if (!captureDevice){ captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; } return captureDevice; } ///////////////////////////////////////////////// //// //// Setup Session,attach Video Preview Layer //// and Capture Device,start running session //// ///////////////////////////////////////////////// -(void) setupCaptureSession { AVCaptureSession *session = [[AVCaptureSession alloc] init]; session.sessionPreset = AVCaptureSessionPresetMedium; AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session]; [self.view.layer addSublayer:captureVideoPreviewLayer]; NSError *error = nil; AVCaptureDevice *device = [self frontFacingCameraifAvailable]; AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; if (!input) { // Handle the error appropriately. NSLog(@"ERROR: trying to open camera: %@",error); } [session addInput:input]; self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG,AVVideoCodecKey,nil]; [self.stillImageOutput setoutputSettings:outputSettings]; [session addOutput:self.stillImageOutput]; [session startRunning]; } ///////////////////////////////////////////////// //// //// Method to capture Still Image from //// Video Preview Layer //// ///////////////////////////////////////////////// -(void) captureNow { AVCaptureConnection *videoConnection = nil; for (AVCaptureConnection *connection in self.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: %@",self.stillImageOutput); __weak typeof(self) weakSelf = self; [self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer,NSError *error) { NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; UIImage *image = [[UIImage alloc] initWithData:imageData]; [weakSelf displayImage:image]; }]; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。