缩放UIImageView以适应屏幕宽度

时间:2012-09-13 10:33:08

标签: objective-c uiimageview

我在UIImageView内使用Storyboard创建了UIViewController

我正在尝试重新缩放图像的宽度以适应屏幕宽度并按比例重新缩放高度:

[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;

然后显示一个缩放的图像,但它适合其他区域,并且我的图像中有一个空白区域。

1 个答案:

答案 0 :(得分:12)

尝试使用此代码:

float imgFactor = frame.size.height / frame.size.width;
frame.size.width = [[UIScreen mainScreen] bounds].size.width;
frame.size.height = frame.size.width * imgFactor;