• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

缩放 UIImageView 以适应屏幕宽度

用户头像
it1352
帮助1

问题说明

我在 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.

正确答案

#1

尝试使用以下代码:

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfgeek
系列文章
更多 icon
同类精品
更多 icon
继续加载