繁体   English   中英

如何相对于原始图像尺寸比例设置UIimageview宽度

[英]how to set UIimageview width with respect to the original image size ratio

在我的iPhone应用程序中,我需要在图像视图中设置图像。

图像视图高度始终为100像素。

we need to set the width of the image view with respect to the original image scale ratio.

也就是说,假设每个图像的宽度X高度为300x400(原始)(4x3)

我们显示的图像视图的图像大小将为75X100

图片的宽度X高度为512x512(原始)(1x1)

我们显示的图像视图的图像大小将为100X100

图片的宽度X高度为128 x 256(原始)(1x2)

我们显示的图像视图的图像大小将为50X100

in all those cases image view height 100pixel should be same, but the width need to varie with respect to the image ratio.

最后,图像视图应位于视图中心

如何实现

有两种方法可以实现此目的。 最简单的方法是在Interface Builder中将图像视图上的缩放模式设置为Aspect Fill,或者

imageView.contentMode = UIViewContentModeScaleAspectFit;

第二种方法是计算图像的纵横比并设置宽度。 就像是:

UIImage *myImage = [UIImage imageNamed:@"MyImage"];
float aspectRatio = myImage.size.width / myImage.size.height;
float viewWidth = 100 * aspectRatio;

好吧,这取决于您的逻辑。
您可以获取UIImage高度和宽度。

 UIImage *image = [UIImage imageNamed:@"anyImage.png"];
 float imageHeight = image.size.height;
 float imageWidth = image.size.width;

之后,您可以计算imageViewWidth-。
根据您的要求-

float value = imageHeight/100.0;
float iV_Width = imageWidth/value;

暂无
暂无

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

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