Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proportionally resize UIImage by width

Tags:

xcode

ios

uiimage

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..

like image 369
LightNight Avatar asked Feb 11 '23 19:02

LightNight


1 Answers

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

  • UIImage+Resize.h
  • UIImage+Resize.m

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];
like image 161
Damian Carrillo Avatar answered Feb 14 '23 08:02

Damian Carrillo