ios UIImageView 填充 UIView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9521443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIImageView fill in UIView
提问by RockBaby
I have a UIImageView
as a subview
of UIView
(frame size 300 x 300). My UIImage
would be either portrait, landscape or some odd sizes. How do I fill in the UIImage
within the UIView
frame size without stretching the image to 300 x 300?
我有一个UIImageView
作为subview
的UIView
(帧大小300×300)。我的UIImage
将是纵向、横向或一些奇怪的尺寸。如何填写UIImage
的内UIView
框的大小而不拉伸图像以300×300?
The purpose of this, I will be adding another UIImageView
as the image border.
Please help.
为此,我将添加另一个UIImageView
作为图像边框。请帮忙。
Thanks.
谢谢。
回答by Srikar Appalaraju
Try
尝试
[img setContentMode:UIViewContentModeScaleAspectFit];
[img setContentMode:UIViewContentModeScaleAspectFit];
UIViewContentModeScaleAspectFit: Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view's bounds is transparent.
UIViewContentModeScaleAspectFit:通过保持纵横比缩放内容以适应视图的大小。视图边界的任何剩余区域都是透明的。
or [img setContentMode:UIViewContentModeScaleAspectFill];
或者 [img setContentMode:UIViewContentModeScaleAspectFill];
UIViewContentModeScaleAspectFill: Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view's bounds.
UIViewContentModeScaleAspectFill:缩放内容以填充视图的大小。内容的某些部分可能会被裁剪以填充视图的边界。
if you are clipping subviews, then you need to do
如果你正在剪辑子视图,那么你需要做
[imgView clipToBounds:YES];
回答by Fang Chen
Ah I just reread what you were asking, I know what your question is now.
啊,我刚刚重读了你的问题,我现在知道你的问题是什么。
Your UIImageView
's frame changes, and when it does so does your image. You don't want your image to change, but you do want your ImageView
to adjust to fill the view it is contained in.
你UIImageView
的框架会改变,当它改变时,你的图像也会改变。您不希望图像发生变化,但确实希望ImageView
进行调整以填充它所在的视图。
UPDATE
更新
I figured it out.
我想到了。
[self.imageView setContentMode:UIViewContentModeCenter];
No matter what orientation, the size stays the same.
无论方向如何,尺寸都保持不变。
You also have the option of setting it to align top, bottom, left, right, top left, top right, bottom left, bottom right, all of which only help to align your image and NOT redraw or re-size the image.
您还可以选择将其设置为对齐顶部、底部、左侧、右侧、左上角、右上角、左下角、右下角,所有这些都只会帮助对齐您的图像,而不是重新绘制或调整图像大小。
Does that help?
这有帮助吗?
回答by Oscar Gomez
Set the contentMode property of UIImageView
to either of this values depending on where you want to put it in the superview:
UIImageView
根据您要将其放在超级视图中的位置,将的 contentMode 属性设置为以下任一值:
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
As this values:
由于此值:
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
Will indeed cause the image to be 300 x 300, which is what you don't want.
确实会导致图像为 300 x 300,这是您不想要的。
回答by calimarkus
Use the contentMode property of UIImageView.
使用 UIImageView 的 contentMode 属性。
Edit:and set the image size to 300x300.
编辑:并将图像大小设置为300x300。
回答by Tomasz Szulc
[image setContentMode:UIViewContentModeScaleToFill];