在 UILabels 后面设置背景相机

Set Background Camera behind UILabels

我已经尝试了很长时间,但无法解决问题,但基本上我想在标签和按钮后面的背景中显示实时摄像头画面。这是我用来使相机出现的代码

   - (void)viewDidLoad {
    [super viewDidLoad];

    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    session.sessionPreset = AVCaptureSessionPresetHigh;

    AVCaptureDevice *device = [AVCaptureDevice  defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    [session addInput:input];

    AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    newCaptureVideoPreviewLayer.frame = self.view.bounds;

    [self.view addSublayer:newCaptureVideoPreviewLayer.view];
    [self.view sendSubviewToBack:newCaptureVideoPreviewLayer.view];

    [session startRunning];

}

我不知道如何将它放在已加载视图的标签后面。任何帮助将不胜感激!

您只需要使用 sendSubviewToBack

在后台添加视图

更多详情请查看Apple AVCam Example

sendSubviewToBack: Moves the specified subview so that it appears behind its siblings.

AVCaptureVideoPreviewLayer *newCaptureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
newCaptureVideoPreviewLayer.frame = self.view.bounds;

[self.view.layer addSublayer:newCaptureVideoPreviewLayer];
[session startRunning];

[[self.view superView] insertSubview:newCaptureVideoPreviewLayer belowSubview:self.view];