[英]How to display a UIImageView that fills the width of the screen, and sets height to keep aspect ratio?
[英]UIImageView with fixed width: how to set its height to keep its aspect?
我有一个视图,我需要放置一个UIImageView
,我被告知将其放在一个矩形内,该矩形占用屏幕宽度,其高度小于宽度(固定大小)。 然而,我给出的图像或多或少是方形jpeg
图像,因此想法是填充应包含图像的矩形的宽度,并以保持其纵横比的方式设置图像的高度。 然后,我应该能够让用户垂直滚动图像以查看完整的图像。
我怎么能这样做?
提前致谢
编辑:我需要为具有不同大小的几个图像执行此操作,但这应该在视图中适合相同的矩形区域大小
您可以设置imageview内容模式。
imageView.contentMode = UIViewContentModeScaleAspectFit;
这将确保通过保持原始宽高比来显示图像。
编辑:
我想这就是你想要的:
UIImage *originalImage = [UIImage imageNamed:[picArr objectAtIndex:a]];
double width = originalImage.size.width;
double height = originalImage.size.height;
double apect = width/height;
double nHeight = 320.f/ apect;
self.img.frame = CGRectMake(0, 0, 320, nHeight);
self.img.center = self.view.center;
self.img.image = originalImage;
希望这可以帮助.. :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.