【问题标题】:AVCaptureVideoPreviewLayer displays nothing on resumeAVCaptureVideoPreviewLayer 在简历上不显示任何内容
【发布时间】:2012-04-12 14:08:49
【问题描述】:

我正在开发一个增强现实应用程序,它使用AVCaptureVideoPreviewLayer 来显示来自设备摄像头的视频源。如果您关闭并重新打开应用程序(通过按下 Home 按钮,而不是强制退出)或让手机进入睡眠状态然后唤醒它,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];

【问题讨论】:

    标签: objective-c ios cocoa-touch avcapturesession


    【解决方案1】:

    您需要使用 KVO 观察 AVCaptureSessioninterrupt 标志并对其做出反应。

    来自AVCaptureSession Reference

    interrupted
    

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

    @property(nonatomic, readonly, getter=isInterrupted) BOOL interrupted
    Discussion
    

    您可以使用键值观察来观察该属性的值

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      相关资源
      最近更新 更多