0

I am trying to resize an image with the maximum of 1000px width but the "ratio" of the original image resolution needs to stay the same. i.e. 1000x1300 or 1000x1600 etc

What changes to I need to make to the code below?

- (void)setImageAndConvertToThumb:(UIImage *)image {
//image
UIImage *sizedImg = [image scaleWithMaxSize:CGSizeMake(1000, 1000) quality:kCGInterpolationHigh];
NSData *data = UIImagePNGRepresentation(sizedImg);
self.image = data;

}
CraigBellamy
  • 27
  • 2
  • 11
  • 1
    possible duplicate of [Resize UIImage with aspect ratio?](http://stackoverflow.com/questions/1703100/resize-uiimage-with-aspect-ratio) – DarkDust Feb 13 '12 at 14:30

1 Answers1

0

The aspect ratio is just the width divided by height (or vice versa), so just calculate the new height by using the same ratio the original image had, like this:

- (void)setImageAndConvertToThumb:(UIImage *)image {
    //image
    CGFloat newWidth = 1000;
    CGFloat newHeight = newWidth * image.size.height / image.size.width;
    UIImage *sizedImg = [image scaleWithMaxSize:CGSizeMake(newWidth, newHeight) quality:kCGInterpolationHigh];
    NSData *data = UIImagePNGRepresentation(sizedImg);
    self.image = data;
}
John Stephen
  • 7,625
  • 2
  • 31
  • 45