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
UIViewContentModeScaleAspectFill not clipping
提问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 contentMode
to UIViewContentModeScaleAspectFill
.
我正在尝试使用UIImageView
. 我已经把我的图像视图的帧大小为100×100,并设置contentMode
到UIViewContentModeScaleAspectFill
。
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 contentMode
to 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?
如果我将 设置contentMode
为UIviewContentModeScaleToFill
,则图像以 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 UIView
containing the UIImageView
I'm working with. I've set their background color to green, and the frame of the view to 100x100. As a test, the UIImageViews
are 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
,它们的大小正确,但图像失真。
回答by imthi
回答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:
总之:
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 thecontentMode
property.Anything outside of a view's bounds is discarded.
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
clipsToBounds
property isYES
on the superview then it will discard subviewcontent outside of its bounds.
光栅化- 所有视图的内容都在视图边界的坐标空间中光栅化(绘制或屏幕外像素缓冲区)。这可能是图像数据或自定义绘图(即
drawRect:
)但子视图内容。这种光栅化考虑了contentMode
属性。视图边界之外的任何内容都将被丢弃。
组合- 结果从子视图到父视图合成(在彼此之上绘制)。这使用了子视图的 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
回答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:
我的子视图正在调整大小,我意识到这是因为 xib 设置了自动布局: