Camera stops delivering images after 22 seconds

NicholasTurnerNicholasTurner ✭✭USMember, University ✭✭

I am using the xamarin example to use the AV api deliver images to a UIView. It works great, but stops at 22 seconds. Can you see anything I'm doing wrong?

`public override void DidOutputSampleBuffer (AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
{
try {
using (var image = ImageFromSampleBuffer (sampleBuffer)) {
sampleBuffer.Dispose ();
sampleBuffer = null;

                    Console.WriteLine("Processing image");
                    // Do something with the image, we just stuff it in our main view.
                    image.InvokeOnMainThread (delegate {
                        if (CamView != null) {
                            CamView.AssignCameraImage(image);
                            //ImageView.Image = image;
                            //ImageView.Transform = CGAffineTransform.MakeRotation ((float)Math.PI / 2);
                        }
                    });

                    //
                    // Although this looks innocent "Oh, he is just optimizing this case away"
                    // this is incredibly important to call on this callback, because the AVFoundation
                    // has a fixed number of buffers and if it runs out of free buffers, it will stop
                    // delivering frames. 
                    //  

                }
            } catch (Exception e) {
                Console.WriteLine (e);
            }
        }

        UIImage ImageFromSampleBuffer (CMSampleBuffer sampleBuffer)
        {
            // Get the CoreVideo image
            using (var pixelBuffer = sampleBuffer.GetImageBuffer () as CVPixelBuffer) {
                // Lock the base address
                pixelBuffer.Lock (0);
                // Get the number of bytes per row for the pixel buffer
                var baseAddress = pixelBuffer.BaseAddress;
                int bytesPerRow = pixelBuffer.BytesPerRow;
                int width = pixelBuffer.Width;
                int height = pixelBuffer.Height;
                var flags = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little;
                // Create a CGImage on the RGB colorspace from the configured parameter above
                using (var cs = CGColorSpace.CreateDeviceRGB ())
                using (var context = new CGBitmapContext (baseAddress, width, height, 8, bytesPerRow, cs, (CGImageAlphaInfo)flags))
                using (var cgImage = context.ToImage ()) {
                    pixelBuffer.Unlock (0);
                    return UIImage.FromImage (cgImage);
                }
            }
        }`

Posts

  • NicholasTurnerNicholasTurner ✭✭ USMember, University ✭✭

    CamView is just a UIView with an imageView. The frame is set to UIScreen.mainScreen.Bounds;

  • TommyOvesenTommyOvesen ✭✭ USMember ✭✭

    Did you find a solution? I have almost the same problem. After a few seconds, the app receives a memory warning, then it crashes silently.

  • nick5454nick5454 ✭✭✭ USUniversity ✭✭✭

    You are not disposing of the images propertly. Paste your code and I can offer suggestions

Sign In or Register to comment.