title: Objective C: How to fix aspect ratio of image (and not change to fit imageview frame)
body: <p>I am using the UIImagePickerController to select images for my app. However I am facing some challenges with regards to maintaining aspect ratio of the selected images</p>
  
  <p>For example,</p>
  
  <p>I have a raw image on the phone as follows (not square image):</p>
  
  <p><img src="https://i.stack.imgur.com/K1GxM.png" alt="enter image description here"></p>
  
  <p>After selecting the image on iphone via the 'move and scale' page to crop image, the result is as follows: (aspect ratio is still maintained here)</p>
  
  <p><img src="https://i.stack.imgur.com/JNyr9.png" alt="enter image description here"></p>
  
  <p>My app will display the selected image in a UIImageView with a square frame (200 x 200). After setting the selected image to the UIImageView (UIImageView.image = selectedImage), the image's aspect ratio is not maintained but instead followed the frame of the UIImageView: (The image looks skewed now in my UIImageView):</p>
  
  <p><img src="https://i.stack.imgur.com/PKPTj.png" alt="enter image description here"></p>
  
  <p>Is there any way to maintain the aspect ratio of the image in this case?</p>
  
  <p>EDIT: Update expected result</p>
  
  <p>After some thinking, I realize that the best way to resolve my issue is to ensure that for such images (non square), I need to 'convert' them into square images i.e. fill the empty spaces on top and bottom of images to make a square e.g.</p>
  
  <p>Expected image: (with the top and bottom borders added)</p>
  
  <p><img src="https://i.stack.imgur.com/bDCKS.png" alt="enter image description here"></p>
  
  <p>EDIT: Update with sample code</p>
  
  <pre><code>- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
  {
  
      self.imageView.contentMode = UIViewContentModeScaleAspectFill;
  
      self.imageView.image = image;
      [[picker parentViewController] dismissModalViewControllerAnimated:YES];
  
      //Compress image
      UIImage *image = [self scaleImage:img toSize:CGSizeMake(612.0,612.0)];
  
      self.imageData = UIImageJPEGRepresentation(image, 0.75);
  }
  </code></pre>
  
  <p>By adding the code "self.imageView.contentMode = UIViewContentModeScaleAspectFill;", I was able to obtain the square image in the uiimageview</p>
  
  <p><img src="https://i.stack.imgur.com/Ghk1k.png" alt="enter image description here"></p>
  
  <p>But it seems like the original image was still not a square image, hence the skewing problem will still occur when I do "UIImage *image = [self scaleImage:img toSize:CGSizeMake(612.0,612.0)];". Is my assumption correct? Anyway to mitigate this issue as I will still need to display the 'compressed' image elsewhere in the app.</p>