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];