AVCaptureVideoPreviewLayer在简历上不显示任何内容

时间:2012-04-12 14:08:49

标签: objective-c ios cocoa-touch avcapturesession

我正在使用增强现实应用,该应用使用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];

1 个答案:

答案 0 :(得分:0)

您需要使用KVO观察interrupt的{​​{1}}标志并对其做出反应。

来自AVCaptureSession Reference

AVCaptureSession

指示接收器是否已被中断。 (只读)

interrupted
  

您可以使用键值观察

来观察此属性的值      

适用于iOS 4.0及更高版本。在AVCaptureSession.h中声明

相关问题