缩放 UIImageView 以适应屏幕宽度 [英] Scaling UIImageView to fit screen width

查看:98
本文介绍了缩放 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;

这篇关于缩放 UIImageView 以适应屏幕宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆