Forum Xamarin Xamarin.iOS

UIImageView fullscreen

Hi All
first of all sorry if my question is the classic newbie request :-)

I have used the tutorial to get the camera stream into an ImageView, all work well but if i want to put the ImageView as a fullscreen and with different displays ( iphone6, iphone6+ etc ) i have some problems.
To be clear i have no idea todo that
this is the stupid code i used,
could you help me?

public override void ViewDidLoad () { base.ViewDidLoad (); CameraView = new UIImageView(); CameraView.Frame = new CGRect (0, 0, View.Bounds.Width, View.Bounds.Height); CameraView.ContentMode = UIViewContentMode.TopLeft; View.AddSubview (CameraView); ThisApp.Recorder.DisplayView = CameraView; }


  • AndreaMucciAndreaMucci USMember

    Hi ashalva

    yes, sure, i read it and this tutorial is very good to know how to get the camera stream and so on, but have no more informations about the UIImageView position.
    In the tutorial the UIImageView size is static and have no methods to modify dinamically the View

  • adamkempadamkemp USInsider, Developer Group Leader mod

    Read this.

  • AndreaMucciAndreaMucci USMember

    Hi adamkemp

    first of all congrats for your blog, i have to take more time to read it entirely :-)
    you are a very pro.

    What you say is correct but something strange happen.
    the ViewDidLayoutSubviews method is never called in my app.
    i put more code to clarify what i do

    i have class that extend AVCaptureVideoDataOutputSampleBufferDelegate to output the Buffered image into the UIImageview

    public class OutputRecorder: AVCaptureVideoDataOutputSampleBufferDelegate
    public UIImageView DisplayView { get; set; }

        public OutputRecorder ()
        private UIImage GetImageFromSampleBuffer(CMSampleBuffer sampleBuffer){
            using (var pixelBuffer = sampleBuffer.GetImageBuffer () as CVPixelBuffer) {
                pixelBuffer.Lock (0);
                var flags = CGBitmapFlags.PremultipliedFirst | CGBitmapFlags.ByteOrder32Little;
                // Decode buffer - Create a new colorspace
                using (var cs = CGColorSpace.CreateDeviceRGB ()) {
                    // Create new context from buffer
                    using (var context = new CGBitmapContext (pixelBuffer.BaseAddress,
                        (CGImageAlphaInfo)flags)) {
                        // Get the image from the context
                        using (var cgImage = context.ToImage ()) {
                            // Unlock and return image
                            pixelBuffer.Unlock (0);
                            return UIImage.FromImage (cgImage);
        public override void DidOutputSampleBuffer (AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
            // Trap all errors
            try {
                // Grab an image from the buffer
                var image = GetImageFromSampleBuffer(sampleBuffer);
                // Display the image
                if (DisplayView !=null) {
                    DisplayView.BeginInvokeOnMainThread(() => {
                        // Set the image
                        DisplayView.Image = image;
                        // Rotate image to the correct display orientation
                        DisplayView.Transform = CGAffineTransform.MakeRotation((float)Math.PI/2);
                // IMPORTANT: You must release the buffer because AVFoundation has a fixed number
                // of buffers and will stop delivering frames if it runs out.
            catch(Exception e) {
                // Report error
                Console.WriteLine ("Error sampling buffer: {0}", e.Message);


    after i have to put some code into the AppDelegate to open camera connection
    so in my AppDelegate.cs i have added this code

    public override void FinishedLaunching (UIApplication application)
    Console.WriteLine ("AppDelegate");
    // Create a new capture session
    Session = new AVCaptureSession ();
    Session.SessionPreset = AVCaptureSession.PresetMedium;

            // Create a device input
            CaptureDevice = AVCaptureDevice.DefaultDeviceWithMediaType (AVMediaType.Video);
            if (CaptureDevice == null) {
                // Video capture not supported, abort
                Console.WriteLine ("Video recording not supported on this device");
                CameraAvailable = false;
            // Prepare device for configuration
            CaptureDevice.LockForConfiguration (out Error);
            if (Error != null) {
                // There has been an issue, abort
                Console.WriteLine ("Error: {0}", Error.LocalizedDescription);
                CaptureDevice.UnlockForConfiguration ();
            // Configure stream for 15 frames per second (fps)
            CaptureDevice.ActiveVideoMinFrameDuration = new CMTime (1, 15);
            // Unlock configuration
            CaptureDevice.UnlockForConfiguration ();
            // Get input from capture device
            Input = AVCaptureDeviceInput.FromDevice (CaptureDevice);
            if (Input == null) {
                // Error, report and abort
                Console.WriteLine ("Unable to gain input from capture device.");
                CameraAvailable = false;
            // Attach input to session
            Session.AddInput (Input);
            // Create a new output
            var output = new AVCaptureVideoDataOutput ();
            var settings = new AVVideoSettingsUncompressed ();
            settings.PixelFormatType = CVPixelFormatType.CV32BGRA;
            output.WeakVideoSettings = settings.Dictionary;
            // Configure and attach to the output to the session
            Queue = new DispatchQueue ("ManCamQueue");
            Recorder = new OutputRecorder ();
            output.SetSampleBufferDelegate (Recorder, Queue);
            Session.AddOutput (output);
            // Let tabs know that a camera is available
            CameraAvailable = true;


    And finally this is my ViewController

    public partial class ViewController : UIViewController
    //private NSError Error;
    //private bool Automatic = true;
    UIImageView CameraView;

        public AppDelegate ThisApp {
            get { return (AppDelegate)UIApplication.SharedApplication.Delegate; }
        public Timer SampleTimer { get; set; }
        public ViewController (IntPtr handle) : base (handle)
        public override void ViewWillAppear (bool animated)
            base.ViewWillAppear (animated);
        public override void ViewDidAppear (bool animated)
            base.ViewDidAppear (animated);
            // Start udating the display
            if (ThisApp.CameraAvailable) {
                // Remap to this camera view
                ThisApp.Recorder.DisplayView = CameraView;
                ThisApp.Session.StartRunning ();
        public override void ViewDidLoad ()
            base.ViewDidLoad ();
            CameraView = new UIImageView();
            CameraView.ContentMode = UIViewContentMode.TopLeft;
            View.AddSubview (CameraView);
            ThisApp.Recorder.DisplayView = CameraView;
        public override void ViewDidLayoutSubviews()
            CameraView.Frame = View.Bounds;
        public override void DidReceiveMemoryWarning ()
            base.DidReceiveMemoryWarning ();        
            // Release any cached data, images, etc that aren't in use.     


    So, the Console.WriteLine into ViewDidLayoutSubviews is never called
    How is possible this?
    have any idea what i have todo?

    thanks in advance for your help

  • adamkempadamkemp USInsider, Developer Group Leader mod

    It sounds like you're never even presenting that view controller. Layout only happens for view controllers that are displayed.

Sign In or Register to comment.