2017-08-05 50 views
1

我試圖在我的視頻預覽圖層上創建一個覆蓋圖。我嘗試了以下。但是我的覆蓋視圖並未顯示在預覽圖層的頂部。它只是顯示一個空白屏幕。覆蓋AVCaptureVideoPreviewLayer

CameraOverlayView包含我的設計

let previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.avCaptureSession) 

    previewLayer.frame = self.view.layer.frame 

    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 

    self.view.layer.addSublayer(previewLayer) 

    let cameraOverlay = CameraOverlay(nibName:"CameraOverlay",bundle: nil) 

    let cameraOverlayView:CameraOverlayView = cameraOverlay.view as! CameraOverlayView 

    self.view.addSubview(cameraOverlayView) 

    cameraOverlayView.layer.addSublayer(previewLayer)   

    self.view.bringSubviewToFront(cameraOverlayView) 

然而,當我加入個人按鈕,預覽層,他們是可見的一個UIView。

但我想展示很多這樣的意見作爲覆蓋,我也想添加constaints。如果我能夠將我的筆尖添加爲覆蓋層,那將會容易得多。

let previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.avCaptureSession) 

    previewLayer.frame = self.view.layer.frame 
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
    self.view.layer.addSublayer(previewLayer) 

    //Add Rectangle 
    var cgRect = CGRectMake(0, 0, previewLayer.frame.width * (3/4) , previewLayer.frame.height) 
    let myView = UIImageView() 
    myView.frame = cgRect 
    myView.backgroundColor = UIColor.clearColor() 
    myView.opaque = false 
    myView.layer.cornerRadius = 10 
    myView.layer.borderColor = UIColor.blackColor().CGColor 
    myView.layer.borderWidth = 3 
    myView.layer.masksToBounds = true 
    //previewLayer.addSublayer(myView.layer) 
    //Add Rectangle 

    //Add Button 
    let captureButton = UIButton(frame: CGRectMake(0, 0, 100 , 100)) 
    captureButton.backgroundColor = UIColor.grayColor() 
    captureButton.setTitle("Capture", forState: .Normal) 
    captureButton.setTitleColor(UIColor.redColor(), forState: .Normal) 
    captureButton.addTarget(self, action: "actionCapture:", forControlEvents: .TouchDragInside) 

    let previewView = UIView(frame: view.frame) 
    view.addSubview(previewView) 
    previewView.layer.addSublayer(previewLayer) 
    view.addSubview(myView) 
    view.addSubview(captureButton) 
    captureButton.bringSubviewToFront(self.view) 
+0

好運。我們知道,當你有問題;-) –

+0

@意義的問題我已編輯並進一步解釋了我的問題。你能檢查一下嗎? – Cloy

回答

0

下工作

let previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.avCaptureSession) 

previewLayer.frame = self.view.layer.frame 

previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 

self.view.layer.addSublayer(previewLayer)   

let cameraOverlay = CameraOverlay(nibName:"CameraOverlay",bundle: nil) 

let cameraOverlayView:CameraOverlayView = cameraOverlay.view as! CameraOverlayView 

let previewView = UIView(frame: view.frame) 

self.view.addSubview(previewView) 

previewView.layer.addSublayer(previewLayer) 

self.view.addSubview(cameraOverlayView) 

self.avCaptureSession?.startRunning()