在我的应用程序中,我正在显示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
}];
captureStillImageAsynchronouslyFromConnection 永远不会为我使用 iPhone5 完成
我已经测试过:
它不是 OS 6,因为此代码适用于已更新的 iPhone 4s 和 iPod(iPod touch(第 4 代)
captureSession 正在运行
videoConnection 不为零
imageOutput 不为零
还:
我使用这种方法而不是 UIImagePickerController 因为我需要将预览作为子视图。
在 iPhone 5 上,在捕获会话上调用stopRunning 也需要几秒钟