简体   繁体   English

如何以编程方式在uiview上拟合图像

[英]How to fit an image on a uiview programmatically

I added a uiview as a subview on a view controller programmatically (called contentView). 我以编程方式在视图控制器上添加了uiview作为子视图(称为contentView)。 I also added an image on that uiview programmatically. 我还以编程方式在该uiview上添加了一张图片。 Every time I run the app on the iPad the image is stretched! 每次我在iPad上运行该应用程序时,图像都会拉伸! How do I fix the image so that it fits the iPad screen but doesn't stretch the image? 如何固定图像以使其适合iPad屏幕但不拉伸图像? I know the drawInRect is what is stretching the image. 我知道drawInRect是拉伸图像。 So how do I fix that? 那么我该如何解决呢?

Here is my code: 这是我的代码:

UIGraphicsBeginImageContextWithOptions(self.contentView.bounds.size, self.contentView.opaque, 0.0);

[[UIImage imageNamed:@"menu image 2.JPG"] drawInRect:self.contentView.bounds];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();  

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

[self.contentView addSubview:imageView];

Change code as below 更改代码如下

UIImage *image = [UIImage imageNamed:@"menu image 2.JPG"];

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

imageView.contentMode = UIViewContentModeScaleAspectFit;

[self.contentView addSubview:imageView];

UIImageView has a property called contentMode which determines how the image layout is handled in the view context. UIImageView具有一个名为contentMode的属性,该属性确定如何在视图上下文中处理图像布局。 contentMode is of type UIViewContentMode . contentMode的类型为UIViewContentMode

The default value is UIViewContentModeScaleToFill which stretches the image without respecting the aspect ratio. 默认值为UIViewContentModeScaleToFill ,它会在不考虑宽高比的情况下拉伸图像。 I am assuming it is the changing aspect ratio that is causing the issue. 我假设是引起此问题的宽高比变化。

If you wish to scale the image, but keep the aspect ratio, you have two options: 如果要缩放图像,但要保持宽高比,则有两个选择:

  1. UIViewContentModeScaleAspectFit : This will show the image scaled to fill the view, but not clip any contents (if the aspect ratio doesn't match view size, it will show either horizontal or vertical bands) UIViewContentModeScaleAspectFit :这将显示缩放后的图像以填充视图,但不剪切任何内容(如果宽高比与视图大小不匹配,它将显示水平或垂直带)

  2. UIViewContentModeScaleAspectFill : This will scale the image to fill the view entirely, without any bands - this will result in content being clipped if the image ratio doesn't match the view ratio. UIViewContentModeScaleAspectFill :这将缩放图像以完全填充视图,没有任何条带-如果图像比例与视图比例不匹配,则会导致内容被剪切。

To set the contentMode on the image view in Objective-C, use the following syntax: 要在Objective-C的图像视图上设置contentMode,请使用以下语法:

UIImage *image = [UIImage imageNamed:@"menu image 2.JPG"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFill;
...

You should not need to use the custom context drawing for this to work anymore (thanks to Losiowaty for asking me about this). 您无需再使用自定义上下文绘图就可以工作了(感谢Losiowaty向我询问此事)。

I guess your self.contentView.bounds is not having the same aspect ratio as your menu image 2.JPG . 我猜你的self.contentView.bounds菜单图像2.JPG的纵横比不同。 For an experiment, please try looking at the menu image 2.JPG 's resolution. 为了进行实验,请尝试查看菜单图片2.JPG的分辨率。 For example if it is 300x150, it's aspect ratio is (300/150 = 2:1) . 例如,如果它是300x150,则其宽高比是(300/150 = 2:1) Set your self.contentView.bounds to this aspect ratio and draw the image and check the results. 将self.contentView.bounds设置为此宽高比,然后绘制图像并检查结果。 It will not stretch. 它不会伸展。

请在您的项目中添加此单行代码

yourImage .contentMode = UIViewContentModeScaleAspectFit;

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

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