ios - 在 UILabels 后面设置背景相机

标签 ios objective-c xcode avcapturesession viewdidload

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

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

}

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

最佳答案

您只需要使用 sendSubviewToBack 在后台添加 View

更多详情可以查看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];

关于ios - 在 UILabels 后面设置背景相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34939665/

相关文章:

iphone - 如何使我的 UIView 的一部分成为可点击区域

ios - 性能 - NSValue 中的结构与容器对象

xcode - 以编程方式启动 OS X 应用程序

ios - 尝试访问从函数发送的类变量时出现 EXC_BAD_ACCESS 错误

android - 如何在包含 PCL + Android + iOS 的项目上使用 Xamarin + MVVMCross 4.2 制作 "Hamburger Menu"?

ios - 如何跟踪缩放的 UIScrollView?

ios - WatchKit 应用程序无法在模拟器上运行

c++ - 在 C++ 和 Objective-C 中使用字符串指针

ios - 带有overlayskscene Xcode Swift 的屏幕触摸Scenekit 导致应用程序崩溃

c - EXC_BAD_ACCESS 用 fscanf 加载 int