在我的应用程序中,我显示了一个AVCaptureVideoPreviewLayer,然后使用AVCaptureOutput中的captureStillImageAsynchronouslyFromConnection函数,在用户点击按钮时捕获静态图像。在iPhone 5上,这一直运行良好,但是它从未完成过。
我的设置代码如下:
我的设置代码如下:
...
self.imageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[self.imageOutput setOutputSettings:outputSettings];
self.captureSession = [[[AVCaptureSession alloc] init] autorelease];
[self.captureSession addInput:self.rearFacingDeviceInput];
[self.captureSession addOutput:self.imageOutput];
[self.captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
self.previewLayer.frame = CGRectMake(0, 0, 320, 427);
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspect;
[self.captureSession startRunning];
[outputSettings release];
我的捕获方法是:
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in self.imageOutput.connections){
for (AVCaptureInputPort *port in [connection inputPorts]){
if ([[port mediaType] isEqual:AVMediaTypeVideo] ){
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}
//code to abort if not return 'soon'
...
[self.imageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error){
//use image here
}];
使用iPhone5时,captureStillImageAsynchronouslyFromConnection方法无法完成
我进行了以下测试:
不是因为操作系统6的原因。因为该代码在已更新的iPhone 4s和iPod(第四代)上都可以工作
captureSession正在运行
videoConnection不为nil
imageOutput不为nil
另外:
我使用这个方法而不是UIImagePickerController,因为我需要将预览放置为子视图。
在iPhone5上,调用capture Session上的stopRunning需要几秒钟时间