swiftmacosavcapturedeviceisight

Display AVCaptureDevice live stream video in Mac Os with Swift


I would like to display a live stream video from my iSight camera.

I was able to detect the AVCaptureDevice that match with the iSight camera, but I don't know how can display the iSight live stream.

I found some examples for iOS (http://jamesonquave.com/blog/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-1/), however I want to achieve the same objective for a Mac OS application.

I also checked in the Apple documentation, but unfortunately some of the methods examples are not available for Swift.

Where should I add the AVCaptureVideoPreviewLayer? Over a CustomView? Or maybe IKCameraDeviceView?

Or can somebody provide me an example about how to use the iSight camera with Swift in Mac Os?


Solution

  • It seems that I found the solution by myself.

    I have to attach AVCaptureVideoPreviewLayer to a CALayer, and then add CALayer into the CustomView.

    I can achieve that in the following way:

    let renderLayer = CALayer()
    
    layer.wantsLayer = true   // layer is a NSView
    layer.layer = renderLayer
    
     // Initialize AVCaptureSession and other stuff...