title: UIImage resize (Scale proportion)
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1703100/resize-uiimage-with-aspect-ratio">Resize UIImage with aspect ratio?</a>  </p>
  </blockquote>
  
  
  
  <p>The following piece of code is resizing the image perfectly, but the problem is that it messes up the aspect ratio (resulting in a skewed image). Any pointers?</p>
  
  <pre><code>// Change image resolution (auto-resize to fit)
  + (UIImage *)scaleImage:(UIImage*)image toResolution:(int)resolution {
      CGImageRef imgRef = [image CGImage];
      CGFloat width = CGImageGetWidth(imgRef);
      CGFloat height = CGImageGetHeight(imgRef);
      CGRect bounds = CGRectMake(0, 0, width, height);
  
      //if already at the minimum resolution, return the orginal image, otherwise scale
      if (width &lt;= resolution &amp;&amp; height &lt;= resolution) {
          return image;
  
      } else {
          CGFloat ratio = width/height;
  
          if (ratio &gt; 1) {
              bounds.size.width = resolution;
              bounds.size.height = bounds.size.width / ratio;
          } else {
              bounds.size.height = resolution;
              bounds.size.width = bounds.size.height * ratio;
          }
      }
  
      UIGraphicsBeginImageContext(bounds.size);
      [image drawInRect:CGRectMake(0.0, 0.0, bounds.size.width, bounds.size.height)];
      UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
  
      return imageCopy;
  }
  </code></pre>