[英]UIImageView with fixed width: how to set its height to keep its aspect?
I have a view where I need to place an UIImageView
, and I was told to place it inside a rectangle that takes the screen width and its height is smaller than width (fixed size). 我有一个视图,我需要放置一个
UIImageView
,我被告知将其放在一个矩形内,该矩形占用屏幕宽度,其高度小于宽度(固定大小)。 However, the images I've been given are more or less square jpeg
images, so the idea is to fill the width of the rectangle that should contain the image and set the height of the image in a way that its aspect ratio is kept. 然而,我给出的图像或多或少是方形
jpeg
图像,因此想法是填充应包含图像的矩形的宽度,并以保持其纵横比的方式设置图像的高度。 Then, I should be able to let the user scroll the image vertically to see the complete image. 然后,我应该能够让用户垂直滚动图像以查看完整的图像。
How could I do this? 我怎么能这样做?
Thanks in advance 提前致谢
EDIT: I need to do this for several images that have different sizes, but that should fit the same rectangular area size within the view 编辑:我需要为具有不同大小的几个图像执行此操作,但这应该在视图中适合相同的矩形区域大小
You can set imageview content mode. 您可以设置imageview内容模式。
imageView.contentMode = UIViewContentModeScaleAspectFit;
This will make sure that the image is displayed by keeping original aspect ratio. 这将确保通过保持原始宽高比来显示图像。
Edit: 编辑:
I think this is what you wanted: 我想这就是你想要的:
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;
Hope this helps.. :) 希望这可以帮助.. :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.