2012-10-25 65 views
1

在我的iPhone应用程序中,我需要在图像视图中设置图像。如何设置相对于原始图像大小比例的UIimageview宽度

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

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

即图像的每假设宽度X高度为300x400(原始)(4×3)

我们显示的图像视图的图像尺寸将是为75x100

宽X高的图像是512x512(原创)(1x1)

我们显示的图像视图的图像尺寸将图像的100X100

宽度X高度128×256(原始)(1×2)

我们显示的图像视图的图像尺寸将是50×

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

和FIN盟友图像视图应在视图中心

如何实现它

回答

5

有一对夫妇,你可以做到这一点的方式。最简单的是设置在图像视图看点缩放模式在界面生成器填充,或

imageView.contentMode = UIViewContentModeScaleAspectFit; 

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

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

那么它取决于你的逻辑。
您可以获得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; 
相关问题