AVCaptureVideoDataOutputSampleBufferDelegate runs for a short time, then stops

  Kiến thức lập trình

I’m trying to do some realtime image processing using Xamarin.iOS C#, but seems like the AVCaptureVideoDataOutputSampleBufferDelegate is hanging after about a dozen samples. If I touch the screen, it will startup for another dozen samples and stops again.

The basis for this code is a Swift example here: https://anuragajwani.medium.com/how-to-process-images-real-time-from-the-ios-camera-9c416c531749 which I converted to a Xamarin project.

public class UICameraPreview : UIView, IAVCaptureVideoDataOutputSampleBufferDelegate
        public AVCaptureSession captureSession = new AVCaptureSession();
        private AVCaptureVideoPreviewLayer previewLayer;
        private AVCaptureVideoDataOutput videoOutput;
        public UICameraPreview(CameraOptions options)
            AddPreviewLayer(); ;

            Task.Run(() =>
        public override void LayoutSubviews()
            previewLayer.Frame = Bounds;

        public void DidOutputSampleBuffer(AVCaptureOutput output, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
            var frame = sampleBuffer.GetImageBuffer();
            if (frame == null)
                Console.WriteLine("unable to get image from sample buffer");
            Console.WriteLine("did receive image frame");

            // process image here


        private void AddCameraInput()

        private void AddPreviewLayer()
            previewLayer = new AVCaptureVideoPreviewLayer(captureSession)
                VideoGravity = AVLayerVideoGravity.ResizeAspect

        private void AddVideoOutput()
            videoOutput = new AVCaptureVideoDataOutput();
            var videoSettings = new NSDictionary<NSString, NSObject>(new NSString("PixelFormatType"), new NSNumber((int)CVPixelFormatType.CV420YpCbCr8BiPlanarFullRange));
            videoOutput.WeakVideoSettings = videoSettings;
            videoOutput.SetSampleBufferDelegateQueue(this, DispatchQueue.MainQueue);


sampleBuffer.GetImageBuffer() works as expected...I just need it to run without hanging.

I have tried sampleBuffer.Dispose() as well but I'm thinking some resource has to be released somewhere.

Nothing I have tried works except adding a delay of 3 seconds or more in the delegate. I believe it some kind memory leak, I am at a loss as to how find a solution.

New contributor

Les Woodland is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.