简体   繁体   English

UIImageView填写UIView

[英]UIImageView fill in UIView

I have a UIImageView as a subview of UIView (frame size 300 x 300 ). 我有一个UIImageView作为UIViewsubview (帧大小300 x 300 )。 My UIImage would be either portrait, landscape or some odd sizes. 我的UIImage将是肖像,风景或一些奇怪的大小。 How do I fill in the UIImage within the UIView frame size without stretching the image to 300 x 300 ? 如何在不将图像拉伸到300 x 300的情况下在UIView帧大小内填写UIImage

The purpose of this, I will be adding another UIImageView as the image border. 为此,我将添加另一个UIImageView作为图像边框。 Please help. 请帮忙。

Thanks. 谢谢。

Try 尝试

[img setContentMode:UIViewContentModeScaleAspectFit];

UIViewContentModeScaleAspectFit: Scales the content to fit the size of the view by maintaining the aspect ratio. UIViewContentModeScaleAspectFit:通过保持纵横比来缩放内容以适合视图的大小。 Any remaining area of the view's bounds is transparent. 视图边界的任何剩余区域都是透明的。

or [img setContentMode:UIViewContentModeScaleAspectFill]; [img setContentMode:UIViewContentModeScaleAspectFill];

UIViewContentModeScaleAspectFill: Scales the content to fill the size of the view. UIViewContentModeScaleAspectFill:缩放内容以填充视图的大小。 Some portion of the content may be clipped to fill the view's bounds. 可以剪裁内容的某些部分以填充视图的边界。

if you are clipping subviews, then you need to do 如果你剪辑子视图,那么你需要这样做

[imgView clipToBounds:YES];

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. 你的UIImageView框架会发生变化,当你的图像发生变化时。 You don't want your image to change, but you do want your ImageView to adjust to fill the view it is contained in. 您不希望更改图像,但确实希望ImageView进行调整以填充其包含的视图。

UPDATE 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? 这有帮助吗?

Set the contentMode property of UIImageView to either of this values depending on where you want to put it in the superview: UIImageView的contentMode属性设置为此值中的任何一个,具体取决于您要将其放在superview中的位置:

 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,这是你不想要的。

Use the contentMode property of UIImageView. 使用UIImageView的contentMode属性。


Edit: and set the image size to 300x300 . 编辑:并将图像大小设置为300x300

 [image setContentMode:UIViewContentModeScaleToFill];

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

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