You will want to review the AVFoundation
APIs (QTKit
is deprecated).
You can create a custom Xamarin.Forms View renderer based on a NSView
and assign the AVCaptureVideoPreviewLayer
as the control's layer to stream the camera output to this control.
Store class level references to following and make sure you Dispose
them when your control goes out of scope otherwise there will be leaks:
AVCaptureDevice device;
AVCaptureDeviceInput input;
AVCaptureStillImageOutput output;
AVCaptureSession session;
In your capture setup, you can grab the default AV device assuming you want to use the build-in FaceTime Camera (also known as iSight
).
macOS/Forms Example:
device = AVCaptureDevice.GetDefaultDevice(AVMediaTypes.Video);
input = AVCaptureDeviceInput.FromDevice(device, out var error);
if (error == null)
{
session = new AVCaptureSession();
session.AddInput(input);
session.SessionPreset = AVCaptureSession.PresetPhoto;
var previewLayer = AVCaptureVideoPreviewLayer.FromSession(session);
previewLayer.Frame = Control.Bounds;
Control.Layer = previewLayer;
output = new AVCaptureStillImageOutput();
session.AddOutput(output);
session.StartRunning();
}

Note: A lot of the AVFoundation
framework is shared between iOS and MacOS, but there are some differences so if you end up looking at iOS sample code be aware you might need to alter it for macOS.