Xamarin iOS 8 , using Auto Layout - Resize images while maintaining the aspect ratio error
<p>I have a challenge working with images in Xamarin iOS 8. I'm using auto layout and have enabled size classes as well. </p>

<p>I have a table view where I am displaying different images from a server. The images vary in dimension, but regardless of that, I would like to always have them resized to fit the bounds of the cell, without distorting them if they are smaller in size.</p>

<p>This is the function I call, to resize the images:</p>

<pre><code>public static UIImage MaxResizeImage(UIImage sourceImage, float maxWidth, float maxHeight)
    {
        var sourceSize = sourceImage.Size;
        var maxResizeFactor = Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
        if (maxResizeFactor &gt; 1)
            return sourceImage;
        var width = maxResizeFactor * sourceSize.Width;
        var height = maxResizeFactor * sourceSize.Height;
        UIGraphics.BeginImageContextWithOptions(new SizeF((float)width, (float)height), false, 2.0f);
        sourceImage.Draw(new RectangleF(0, 0, (float)width, (float)height));
        var resultImage = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
        return resultImage;
    }
</code></pre>

<p>Challenge:</p>

<ul>
<li><p>Using the <strong>ScaleAspectFit</strong> content mode on the UIImageView on the cell,</p>

<ul>
<li><p>On the iPhone, if the image is smaller than the width of the screen, there are blank spaces.</p></li>
<li><p>On the iPad, they all have blank spaces on the sides.</p></li>
</ul></li>
</ul>

<p><strong>Aspect Fit iPhone</strong></p>

<p><a href="https://i.stack.imgur.com/i8LjR.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i8LjR.jpg" alt="iPhone sample - Scale AspectFit in Portrait mode"></a></p>

<p><strong>Aspect Fit iPad</strong></p>

<p><a href="https://i.stack.imgur.com/wOENG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wOENG.jpg" alt="AspectFit iPad Portrait mode"></a></p>

<ol>
<li>I changed the Content Mode to <strong>ScaleAspectFill</strong></li>
</ol>

<p><strong>AspectFill iPhone portrait mode</strong></p>

<p>They look okay in portrait mode on the iPhone
 <a href="https://i.stack.imgur.com/X07Z6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/X07Z6.jpg" alt="enter image description here"></a></p>

<p><strong>AspectFill iPad portrait mode</strong></p>

<p>They look cut off here and sometimes overlap.
<a href="https://i.stack.imgur.com/o3KBi.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o3KBi.jpg" alt="enter image description here"></a></p>

<p><strong>AspectFill iPad landscape mode</strong></p>

<p>They look cut off here and sometimes overlap.</p>

<p><a href="https://i.stack.imgur.com/fuYsQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fuYsQ.jpg" alt="enter image description here"></a></p>

<p>I tested uploading the same images on Facebook and regardless of the size if the image,it always gets re-sized to fit the width, while the aspect ratio is maintained.</p>

<p>How do I resize the images properly regardless of the device? What content mode is suitable?</p>

<p><strong>EDIT GetHeightForRow override</strong></p>

<p>This is how I'm calculating the height of each image.</p>

<pre><code>public override nfloat GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
    {

        SampleCell cell = tableView.DequeueReusableCell (SampleCell.Key) as SampleCell;

        if (cell == null) {

            cell = new SampleCell ();

        }

        nfloat height = 0.0f;

        try {


            if (cell != null) 
            {
                var _url = TableItems[indexPath.Row].Item4;


                //call method to load the images
                if (_url != null)
                {
                    cell.cellImageView.SetImage(
                        url: new NSUrl(_url.AbsoluteUri),
                        placeholder: UIImage.FromBundle("sample.png"),
                        completedBlock: (image,error,type,url)=&gt; {

                            if(image != null)
                            {
                                imageResizer = new ImageResizer(image);

                                imageResizer.RatioResize((float)tableView.Frame.Size.Width,(float)tableView.Frame.Size.Height);
                                UIImage resizedImage = imageResizer.ModifiedImage;

                                cell.cellImageView.Image = image;

                            }


                        }
                    );

                    height = cell.ContentView.SystemLayoutSizeFittingSize (UIView.UILayoutFittingCompressedSize).Height;
                    height += 1;

                }

 }


        } catch (Exception ex) {

            Console.WriteLine (ex.Message + ex.StackTrace);

        }
        //return 240f;
        return height;

    }
</code></pre>