2016-01-22 86 views
0

我一直在嘗試很長時間,無法讓它解決,但基本上我想在背景後面顯示實時攝像頭源標籤和按鈕。這裏是我正在使相機出現的代碼在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]; 

} 

我不知道如何將它放在視圖上的標籤後面加載。任何幫助將非常感激!

+0

'[self.view.layer sendSubviewToBack:newCaptureVideoPreviewLayer]'嘗試設置背景 –

回答

2

你只需要使用sendSubviewToBack

更多細節添加視圖在後臺,您可以檢查Apple AVCam Example

sendSubviewToBack: 移動指定的子視圖,使其顯示它的兄弟姐妹後面。

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

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

OR

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

視圖時,我添加此代碼,我得到那個說「不可見的錯誤'CALayer'的接口聲明瞭選擇器'sendSublayerToBack:'「我只在視圖中實現了AVCapture會話,因此我沒有在其他地方聲明 – spe

+0

@spel檢查我更新的ans –

+0

現在即時通訊沒有可見的@interface 'UIView'聲明選擇器'addSublaye r:'和不兼容的指針類型將'AVCaptureVideoPreviewLayer *'發送給'UIView * _Nonnull'類型的參數我也嘗試在我的.h文件中連接視圖並將其標記爲cameraview,但是當我運行該應用程序時,它崩潰了,這是一種痛苦!我也感謝你的幫助 – spe