4

I need to resize UIImage proportionally by width, may be someone have a working code?

For ex.:

Input: Width: 900 Height: 675. Resize to width: 400 width

Result: Width: 400 Height: 300

Please help..

LightNight
  • 1,616
  • 6
  • 30
  • 59

2 Answers2

7

Check this blog post out. In particular, these two files:

Maybe this would work for you:

UIImage *image = [UIImage imageNamed:@"SomeImage-900x675"]; // SomeImage-900x675.png
CGFloat targetWidth = 400.0f;

CGFloat scaleFactor = targetWidth / image.size.width;
CGFloat targetHeight = image.size.height * scaleFactor;
CGSize targetSize = CGSizeMake(targetWidth, targetHeight);

UIImage *scaledImage = [image resizedImage:targetSize interpolationQuality:kCGInterpolationHigh];
Damian Carrillo
  • 1,218
  • 9
  • 11
1

Couldn't you just set the content mode? to .ScaleAspectFill? Then you can set the UIImageView size to any arbitrary size and the aspect ratio will be preserved.

https://developer.apple.com/reference/uikit/uiviewcontentmode/1622518-scaleaspectfill

meds
  • 21,699
  • 37
  • 163
  • 314