简体   繁体   中英

Resize image and maintain its aspect ratio in iOS

I am trying to select an image from photo library, this is the photo 在此处输入图片说明 which I have downloaded from net and is stored in my photo lib.

So now I want to give user an option to select an image from photo lib and apply as a backgroundImage throught my app, but when I do it, I get the image this way on my iphone screen

在此处输入图片说明

Actually the image has been filled the screen, because the content mode specified is "scaleToFill".

1) I want to know how to make this image maintain its aspect ratio and also fill the screen?

Regards Ranjit

check this blog . use these two files

UIImage+Resize.h

UIImage+Resize.m

UIImage *image = [UIImage imageNamed:@"SomeImage-900x675"]; // SomeImage-900x675.png 
CGFloat targetWidth = 320.0;
CGFloat scaleFactor = targetWidth / image.size.width; 
CGFloat targetHeight = image.size.height * scaleFactor; 
CGSize targetSize = CGSizeMake(targetWidth, targetHeight);
UIImage *scaledImage = [image resizedImage:targetSize interpolationQuality:kCGInterpolationHigh];

尽管您的图片占据了所有屏幕尺寸,但是您的图片尺寸却更大了...您可以尝试按以下分辨率缩放图片320 * 480、640 * 960、1240 * 2208

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM