简体   繁体   English

具有固定宽度的UIImageView:如何设置其高度以保持其宽高?

[英]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.

相关问题 如何显示一个UIImageView来填充屏幕的宽度,并设置高度以保持纵横比? - How to display a UIImageView that fills the width of the screen, and sets height to keep aspect ratio? 如何将视图高度设置为等于其宽度 - How to set a view height to be equal to its width 如何在保持其纵横比的同时调整AsyncImageView(UIImageView的子类)的大小 - How to resize AsyncImageView (subclass of UIImageView) while maintaining its Aspect Ratio UIImageView-固定宽度和灵活高度? - UIImageView - Fixed width & Flexible height? UIImageView 保持纵横比,但适合宽度 - UIImageView keep aspect ratio, but fit to width 如何调整UITextView的大小,当达到一定值时,它会根据内容和固定宽度自动调整其宽度和高度? - How to resize a UITextView that automatically resize its width and height based on the content and fixed width when reaches certain value? 设置UIImage的高度等于其宽度 - Set UIImage's height equal to its width 如何以编程方式将UIImageView缩放为固定宽度和灵活高度? - How to programmatically scale a UIImageView to a fixed width and flexible height? 如何使用自动布局调整固定宽度和高度的UIImageView的大小 - How to resize a fixed width and height UIImageView with auto layout 如何以 UIImageView 作为其子级来制作水平 UIStackView 换行高度内容? - How to make horizontal UIStackView wrap height content with UIImageView as its children?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM