title: Making square crop of UIImage causing image to stretch
body: <p>When attempting to perform a center crop of an UIImage, I get the following results (left is the original image at <code>640*1136</code>, and the right is the cropped image fitted to a square <code>UIImageView</code> at <code>320*320</code>):</p>
  
  <p><img src="https://i.stack.imgur.com/NbmWV.png" alt="enter image description here"> turns to: 
  <img src="https://i.stack.imgur.com/D3Bnj.png" alt="enter image description here"></p>
  
  <p>I have tinkered quite a bit with the ratio element so that it could correctly detect the amount to trim: using the shorter side of of the image, constructing a ratio based on <code>short_side/width_of_desired_rect</code>, but it does not appear to be working in this case. Help appreciated!</p>
  
  <pre><code>- (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
      double ratio;
      double delta;
      CGPoint offset;
  
      //make a new square size, that is the resized imaged width
      CGSize sz = CGSizeMake(newSize.width, newSize.width);
  
      //figure out if the picture is landscape or portrait, then
      //calculate scale factor and offset
      if (image.size.width &gt; image.size.height) {
          ratio = newSize.width / image.size.width;
          delta = (ratio*image.size.width - ratio*image.size.height);
          offset = CGPointMake(delta/2, 0);
      } else {
          ratio = newSize.width / image.size.height;
          delta = (ratio*image.size.height - ratio*image.size.width);
          offset = CGPointMake(0, delta/2);
      }
  
      //make the final clipping rect based on the calculated values
      CGRect clipRect = CGRectMake(-offset.x, -offset.y,
                                   (ratio * image.size.width) + delta,
                                   (ratio * image.size.height) + delta);
  
      //for retina consideration
      if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
          UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
      } else {
          UIGraphicsBeginImageContext(sz);
      }
      UIRectClip(clipRect);
      [image drawInRect:clipRect];
      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
  
      return newImage;
  }
  </code></pre>