title: Resize UIImage and change the size of UIImageView
body: <p>I have this <code>UIImageView</code> and I have the values of its max height and max width. What I want to achieve is that I want to take the image (with any aspect ratio and any resolution) and I want it to fit in the borders, so the picture <em>does not exceed</em> them, but it can shrink them as it wants. (marked red in the picture):</p>
  
  <p><img src="https://i.stack.imgur.com/qLfgv.png" alt="enter image description here"></p>
  
  <p>Right now the image fits the necessary size properly, but I have 2 worries:
  1. The <code>UIImageView</code> is not equal the size of the resized image, thus leaving red background (and I don't want that)
  2. If the image is smaller that the height of my <code>UIImageView</code> it is not resized to be smaller, it stays the same height.</p>
  
  <p>Here's my code and I know its wrong:</p>
  
  <pre><code>UIImage *actualImage = [attachmentsArray lastObject];
  UIImageView *attachmentImageNew = [[UIImageView alloc] initWithFrame:CGRectMake(5.5, 6.5, 245, 134)];
  attachmentImageNew.image = actualImage;
  attachmentImageNew.backgroundColor = [UIColor redColor];
  attachmentImageNew.contentMode = UIViewContentModeScaleAspectFit;
  </code></pre>
  
  <p>So how do I dynamically change the size not only of the <code>UIImageView.image</code>, but of the whole <code>UIImageView</code>, thus making its size totally adjustable to its content. Any help would be much appreciated, thanks!</p>