Resizes the UIImage proportionally to its width
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 to share
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 to share