using AVFoundation; using CoreFoundation; using CoreGraphics; using CoreVideo; using Demo.Controls; using Foundation; using System.Linq; using UIKit; namespace Demo.iOS.Controls { public class UICameraPreview : UIView { private AVCaptureVideoPreviewLayer previewLayer; private CameraOptions cameraOptions; public AVCaptureSession CaptureSession { get; private set; } public bool IsPreviewing { get; set; } public UICameraPreview(CameraOptions options) { cameraOptions = options; IsPreviewing = false; Initialize(); } public override void Draw(CGRect rect) { base.Draw(rect); previewLayer.Frame = rect; } private void Initialize() { CaptureSession = new AVCaptureSession(); previewLayer = new AVCaptureVideoPreviewLayer(CaptureSession) { Frame = Bounds, VideoGravity = AVLayerVideoGravity.ResizeAspectFill }; var videoDevices = AVCaptureDevice.DevicesWithMediaType(AVMediaType.Video); var cameraPosition = (cameraOptions == CameraOptions.Front) ? AVCaptureDevicePosition.Front : AVCaptureDevicePosition.Back; var device = videoDevices.FirstOrDefault(d => d.Position == cameraPosition); if (device == null) { return; } var input = new AVCaptureDeviceInput(device, out NSError error); CaptureSession.AddInput(input); // var output = new AVCaptureVideoDataOutput();//CVPixelFormatType.CV32BGRA output.WeakVideoSettings = new CVPixelBufferAttributes { PixelFormatType = CVPixelFormatType.CV32BGRA }.Dictionary; output.SetSampleBufferDelegateQueue(new SampleBufferDelegate(), new DispatchQueue("myQueue")); CaptureSession.AddOutput(output); // Layer.AddSublayer(previewLayer); CaptureSession.StartRunning(); IsPreviewing = true; } } }