我正在开发一个增强现实应用程序,它使用AVCaptureVideoPreviewLayer
来显示来自设备摄像头的视频源。如果您关闭并重新打开应用程序(通过按下主页按钮,而不是强制退出)或让手机进入睡眠状态然后唤醒它,则会AVCaptureVideoPreviewLayer
显示一个黑色矩形而不是相机的提要。
我正在初始化捕获会话,如下所示viewDidLoad:
captureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if (videoDevice) {
NSError *error;
AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if (!error) {
if ([captureSession canAddInput:videoIn])
[captureSession addInput:videoIn];
else
NSLog(@"Couldn't add video input");
} else
NSLog(@"Couldn't create video input");
} else
NSLog(@"Couldn't create video capture device");
[captureSession startRunning];
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
previewLayer.frame = cameraView.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[[cameraView layer] addSublayer:previewLayer];