UIViewContentModeScaleAspectFill不剪切 [英] UIViewContentModeScaleAspectFill not clipping
问题描述
我正在尝试使用 UIImageView
以固定大小(100x100)绘制一些缩略图。我已将图像视图的帧大小设置为100x100,并将 contentMode
设置为 UIViewContentModeScaleAspectFill
。
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
.
我的理解是这应该导致图像以我设置的尺寸绘制,图像将填充图像的区域,并剪切图像的任何部分超出我为图像视图设置的大小。但是,图像仍然比我为其设置的100x100大小更大或更小。
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.
如果我设置 contentMode
到 UIviewContentModeScaleToFill
,然后图像精确地以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?
这是我的代码:
_photoView = [[UIImageView alloc] initWithImage:photoImage];
_photoView.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview:_photoView];
CGRect photoFrame = _photoView.frame;
photoFrame.size = CGSizeMake(100, 100);
_photoView.frame = photoFrame;
为了更好地说明,这里是我所看到的截图。绿色方块是 UIView
,其中包含我正在使用的 UIImageView
。我将它们的背景颜色设置为绿色,并将视图的帧设置为100x100。作为测试, UIImageViews
的大小为50x50。如您所见,当使用 ... AspectFill
时,图像的大小不会达到50x50,在某些情况下会偏离我喜欢的位置。当使用 ... ScaleToFill
时,它们的大小正确,但图像失真。
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.
推荐答案
你能尝试设置剪辑到界限
Can you try setting clip to bounds
[_photoview setClipsToBounds:YES];
如果可以的话,直接在你的Storyboard / Xib中:
Or directly in your Storyboard / Xib if you can :
这篇关于UIViewContentModeScaleAspectFill不剪切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!