简体   繁体   English

ImageView限制宽度但不限制高度

[英]ImageView to constraint the width but not the height

I have an Image and an ImageView, the ImageView occupies the whole screen and I want to resize the image to fit the ImageView. 我有一个Image和一个ImageView,ImageView占据了整个屏幕,并且我想调整图像的大小以适合ImageView。 The ImageView is inside a ScrollView with only vertical scroll bar enabled, which means the Image should be resized with maximum width equal to that of the screen(ImageView), in the mean time keeping the ratio, not constrained on the resized height. ImageView在ScrollView内,并且只启用了垂直滚动条,这意味着应调整Image的大小,使其最大宽度等于屏幕的最大宽度(ImageView),同时保持比例,而不受大小调整后的高度的限制。 The ImageView's height will fit with resized Image, if the height exceeds the height of the screen we can use the scroll bar to see it. ImageView的高度将与调整大小的Image匹配,如果高度超过屏幕的高度,我们可以使用滚动条查看它。

So I want suggestions on: 因此,我需要以下建议:

1) How I can resize an Image with a fixed width if the original width is less than the desired one, no resize is required. 1)如果原始宽度小于所需宽度,如何调整固定宽度的图像,则无需调整大小。 No constraint on height. 高度不受限制。

2) How I can make the ImageView fit the height of the Image automatically? 2)如何使ImageView自动适应Image的高度?

First, you can calculate the new image's height by yourself. 首先,您可以自己计算新图像的高度。

(if, image's size = (300,200), imageView's width = 320, the new image's height = 200*(320/300)) (如果图像的大小=(300,200),imageView的宽度= 320,新图像的高度= 200 *(320/300))

Then, from this thread The simplest way to resize an UIImage? 然后,从此线程中调整UIImage大小的最简单方法是?

You can resize the Image with new width and height. 您可以使用新的宽度和高度来调整图像的大小。


Total soultion: 总灵魂:

UIImage* i= /* original image */;
UIImage* j =[self imageWithImage:i scaledToSize:newSize]; //new Image
[theImageView setImage:j];

(And this code also works fine with iOS 4.x) (并且此代码在iOS 4.x上也可以正常工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM