缩放 UIImageView 以适应屏幕宽度
问题描述:
我在 UIViewController
中使用 Storyboard 创建了一个 UIImageView
.
I have an UIImageView
created using Storyboard inside UIViewController
.
我正在尝试重新缩放图像的宽度以适应屏幕宽度并按比例重新缩放高度:
I'm trying to rescale the width of the image to fit the screen width and rescale height proportionally:
[fImage setImage:[UIImage imageNamed: @"f2345.jpeg"]];
[fImage sizeToFit];
fImage.contentMode = UIViewContentModeScaleAspectFit;
CGRect frame = filmImage.frame;
frame.size.width = [[UIScreen mainScreen] bounds].size.width;
// Alternative way to do the similar thing - gives the same result in my case
// frame.size.width = [[UIScreen mainScreen] applicationFrame].size.width;
fImage.frame = frame;
然后它会显示一个经过缩放但适合其他区域的图像,并且在我的图像上有一个空白.
It then shows an image which is scaled but it fits some other area and there is a whitespace aruond my image.
答
尝试使用以下代码:
float imgFactor = frame.size.height / frame.size.width;
frame.size.width = [[UIScreen mainScreen] bounds].size.width;
frame.size.height = frame.size.width * imgFactor;