Resizes the UIImage proportionally to its width

I need to resize UIImage

proportionally in width, maybe someone has a working code?

For example:

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

Result: Width: 400 Height: 300

Please, help..

+3


source to share


2 answers


Check out this blog post . Specifically, these two files:



Maybe this will 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];

      

+6


source


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



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

+1


source







All Articles