ios - 如何在 AVCaptureVideoPreviewLayer 上显示覆盖

标签 ios objective-c avfoundation avcapturesession uiview-hierarchy

我需要在 AVCaptureVideoPreviewLayer 上显示图像(帧)叠加层。 我该怎么做?

-(void) viewDidAppear:(BOOL)animated
{
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;
    [session commitConfiguration];

    CALayer *viewLayer = self.vImagePreview.layer;
    NSLog(@"viewLayer = %@", viewLayer);

    AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

    captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
    [self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (!input) {
        // Handle the error appropriately.
        NSLog(@"ERROR: trying to open camera: %@", error);
    }
    [session addInput:input];

    _stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
    NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
    [_stillImageOutput setOutputSettings:outputSettings];

    [session addOutput:_stillImageOutput];

    [session startRunning];

}

在视频输出层上添加新 View 不起作用。 预览层显示在所有 View 之上。

我的 View 层次结构:

  • 主视图
    • 叠加 View
    • 视频输出 View

My frame appears under the video output view

最佳答案

虽然这个问题是 N 年前提出的,但我还是想分享我的确切答案。几分钟后,我能够理解古杰明的话。事实上,解决方案可以通过编程方式完成。但如果您在 XIB 或 Storyboard 中进行操作,请检查这种层次结构。不要将任何 subview 放入您的 CameraPreviewView(其图层将成为相机的 uiview)。

enter image description here

关于ios - 如何在 AVCaptureVideoPreviewLayer 上显示覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17965635/

相关文章:

ios - 返回 null 的 NSData dataWithContentsOfFile

iOS 如何将 IB 自定义类设置为类别

iphone - 在Xcode 4.5中构建AVFoundation.framework时出现编译错误

objective-c - 存储绝对 NSDate,而不是相对于时区

ios - 从麦克风录音和处理(PCM?)文件的示例

ios - 如何使带有 AVCaptureVideoPreviewLayer 的 UIView 看起来更好?

ios - 在动画 UIView 上点击手势不起作用

ios - 将表单条目发送到数据库

objective-c - Objective C、Cocoa、参数引用 - ITK #Cocoa

ios - 在 XCode 6.1.1 中调试 EXC_BAD_INSTRUCTION 崩溃