ios UIViewContentModeScaleAspectFill 不剪裁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6638623/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:41:07  来源:igfitidea点击:

UIViewContentModeScaleAspectFill not clipping

iosuiimageview

提问by Josh Buhler

I'm trying to draw some thumbnail images at a fixed size (100x100) using UIImageView. I've set the frame size of my image view to be 100x100, and set the contentModeto UIViewContentModeScaleAspectFill.

我正在尝试使用UIImageView. 我已经把我的图像视图的帧大小为100×100,并设置contentModeUIViewContentModeScaleAspectFill

My understanding is that this should cause the image to be drawn at the size I set, and the image will fill the area of the image, and clip any portions of the image that are outside the size I set for the image view. However, the image is still drawn larger or smaller than the 100x100 size that I set for it.

我的理解是,这应该会导致以我设置的大小绘制图像,并且图像将填充图像的区域,并裁剪图像中超出我为图像视图设置的大小的任何部分。但是,图像仍然比我为其设置的 100x100 尺寸更大或更小。

If I set the contentModeto UIviewContentModeScaleToFill, then the image is drawn at exactly 100x100, but it's distorted to fit into those dimensions. Any ideas why the aspect fill isn't clipping as expected?

如果我将 设置contentModeUIviewContentModeScaleToFill,则图像以 100x100 精确绘制,但它会扭曲以适应这些尺寸。任何想法为什么方面填充没有按预期裁剪?

Here's my code:

这是我的代码:

_photoView = [[UIImageView alloc] initWithImage:photoImage];
_photoView.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview:_photoView];

CGRect photoFrame = _photoView.frame;
photoFrame.size = CGSizeMake(100, 100);
_photoView.frame = photoFrame;

To better illustrate, here's an screenshot of what I'm seeing. The green squares are the UIViewcontaining the UIImageViewI'm working with. I've set their background color to green, and the frame of the view to 100x100. As a test, the UIImageViewsare sized to 50x50. As you can see, when using the ...AspectFill, the images aren't sized to 50x50, and in some cases are offset from where I'd like them. When using ...ScaleToFill, they're sized correctly, but the image is distorted.

为了更好地说明,这是我所看到的截图。绿色方块是UIView包含UIImageView我正在使用的。我将它们的背景颜色设置为绿色,并将视图的框架设置为 100x100。作为测试,UIImageViews尺寸为 50x50。正如您所看到的,当使用 时...AspectFill,图像的大小不是 50x50,并且在某些情况下会偏离我想要的位置。使用 时...ScaleToFill,它们的大小正确,但图像失真。

Screenshot illustrating problem

说明问题的屏幕截图

回答by imthi

Can you try setting clip to bounds

您可以尝试将剪辑设置为边界吗

[_photoview setClipsToBounds:YES];

Or directly in your Storyboard / Xib if you can :

如果可以,或者直接在您的故事板/ Xib 中:

enter image description here

在此处输入图片说明

回答by Robert

If you want to expand your knowledge there is a really good article on how the iOS UIView stack is rendered. There is also a more in-depth article about the whole drawing pipeline.

如果你想扩展你的知识,有一篇关于如何呈现 iOS UIView 堆栈的非常好的文章。还有一篇关于整个绘图管道的更深入的文章。

In summary:

总之:

  1. Rasterisation- All the view's content is rasterised (drawn or a offscreen pixel buffer) in coordinate space of the View's bounds. This could be image data or custom drawing (i.e. drawRect:) but subview content. This rasterisation takes into account the contentModeproperty.

    Anything outside of a view's bounds is discarded.

  2. Composition- The results are composited (drawn on top of each other) from subview to superviews. This uses the frame property of the subview.

    If the clipsToBoundsproperty is YESon the superview then it will discard subviewcontent outside of its bounds.

  1. 光栅化- 所有视图的内容都在视图边界的坐标空间中光栅化(绘制或屏幕外像素缓冲区)。这可能是图像数据或自定义绘图(即drawRect:)但子视图内容。这种光栅化考虑了contentMode属性。

    视图边界之外的任何内容都将被丢弃。

  2. 组合- 结果从子视图到父视图合成(在彼此之上绘制)。这使用了子视图的 frame 属性。

    如果该clipsToBounds属性YES在超级视图上,那么它将丢弃其边界之外的子视图内容。

回答by Brackston Mayhall

had the same problem and it was resolved by ticking "Clip Subviews".

有同样的问题,它是通过勾选“剪辑子视图”解决的。

The image was showing up properly for all views (3.5,4,4.7,5.5,ipad) in storyboard preview but not in the simulator.

图像在故事板预览中的所有视图(3.5、4、4.7、5.5、ipad)中都正确显示,但在模拟器中却没有。

After I ticked "Clip Subviews" the problem was resolved. :)

在我勾选“剪辑子视图”后,问题就解决了。:)

回答by Maxime T

Checking "Clip subviews" directly in the Storyboard/Xib also worked for me. enter image description here

直接在 Storyboard/Xib 中检查“剪辑子视图”也对我有用。 在此处输入图片说明

回答by bharathi kumar

If everything fails,

如果一切都失败了,

do the clips to bounds in viewDidLayoutSubviews Method.

在 viewDidLayoutSubviews 方法中对边界进行剪辑。

Example :

例子 :

  override func viewDidLayoutSubviews() {

    imageProfile.layer.cornerRadius = imageProfile.bounds.size.width/2
    imageProfile.clipsToBounds = true
    imageProfile.layer.masksToBounds = true
 }

回答by alistair

My subviewes were resizing and I realised it was because the xib had auto layout set: enter image description here

我的子视图正在调整大小,我意识到这是因为 xib 设置了自动布局: 在此处输入图片说明