Overlay over AVCaptureVideoPreviewLayer

时间:2017-08-05 10:34:57

标签: ios swift uiview overlay avcapture

I am trying to create an overlay over my video preview layer. I tried the following. But my overlay view is not being displayed on top of the preview layer. It simply shows a blank screen.

CameraOverlayView contains my design

    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)

However when i am adding individual button and a UIView on the preview layer they are visible.

But i want to dispay a lot of such views as an overlay and i also want to add constaints. It would be much easier if i would be able to add my nib as an overlay.

    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)

1 个答案:

答案 0 :(得分:1)

以下工作

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()