简体   繁体   English

在iOS中调整图像大小并保持其纵横比

[英]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 所以现在我想给用户一个选项,可以从照片库中选择一个图像,并在我的应用程序中作为backgroundImage进行应用,但是当我这样做时,我会以这种方式在iphone屏幕上获取图像

在此处输入图片说明

Actually the image has been filled the screen, because the content mode specified is "scaleToFill". 实际上,由于指定的内容模式为“ scaleToFill”,因此图像已经填满了屏幕。

1) I want to know how to make this image maintain its aspect ratio and also fill the screen? 1)我想知道如何使此图像保持其纵横比并填充屏幕?

Regards Ranjit 问候兰吉特

check this blog . 检查这个博客 use these two files 使用这两个文件

UIImage+Resize.h 的UIImage + Resize.h

UIImage+Resize.m 的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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