简体   繁体   English

UIImageView contentMode Scale Aspect Fill 正在拉伸图像(没关系,它不是)

[英]UIImageView contentMode Scale Aspect Fill is Stretching the Image (Nevermind, It Wasn't)

I feel like I'm going crazy.我觉得我快疯了。 I set my UIImageView.contentMode = UIViewContentModeScaleAspectFill, but it is still stretching the image.我设置了我的 UIImageView.contentMode = UIViewContentModeScaleAspectFill,但它仍在拉伸图像。 (Not a clipping issue, it is stretching the image.) (不是剪辑问题,它正在拉伸图像。)

It even shows it stretched in my Storyboard.它甚至显示它在我的 Storyboard 中拉伸。 I created an image of green and brown squares to test:我创建了一个绿色和棕色方块的图像来测试:

在此处输入图像描述

I added a UIImageView in my Storyboard.我在我的 Storyboard 中添加了一个 UIImageView。 I added the image to that image view, and when I'm setting to Aspect FIT then it does what it is supposed to do (maintain aspect ratio and show the whole image allowing gaps as needed), but when I switch to Aspect FILL then it stretches the image.我将图像添加到该图像视图中,当我设置为 Aspect FIT 时,它会执行它应该做的事情(保持纵横比并根据需要显示整个图像允许间隙),但是当我切换到 Aspect FILL 时它拉伸图像。 Here is an animated gif from my storyboard:这是来自我的 storyboard 的动画 gif:

在此处输入图像描述

What the heck is going on?到底他妈发生了什么? This is in a storyboard in a project that is a few years old.这是一个几年前的项目中的 storyboard。 If I create a brand new project with a brand new storyboard, then this insanity doesn't happen.如果我使用全新的 storyboard 创建一个全新的项目,那么这种疯狂就不会发生。

Is there something in my underlying storyboard that is causing this bug?我的底层 storyboard 中是否存在导致此错误的内容?

Any help is appreciated.任何帮助表示赞赏。 I feel like I'm taking crazy pills.我觉得我正在服用疯狂的药丸。

It's not stretching it.它没有拉伸它。 It's filling the area while keeping the aspect ratio.它在保持纵横比的同时填充该区域。 If it were stretching it, the horizontal white line in the middle would not be as tall as the vertical white lines are wide.如果拉伸的话,中间的水平白线不会像垂直的白线那么宽。

Set the content mode to "Scale To Fill" to see what it would look like if it were getting stretched (like this):将内容模式设置“缩放填充”以查看拉伸后的效果(如下所示):

缩放填充

Note the difference in line thickness of the middle white line, as opposed to when it's set to Aspect Fill:请注意中间白线的线条粗细差异,而不是设置为 Aspect Fill 时:

启用剪辑到边界的纵横比填充

If you turn off Clip to Bounds on the image view, then you can see how the image is getting cropped when set to Aspect Fill:如果您在图像视图上关闭 Clip to Bounds,那么您可以看到设置为 Aspect Fill 时图像是如何被裁剪的:

禁用剪辑到边界的纵横比填充

If you want to show the full image at the full width while keeping the aspect ratio, you'll have to increase that cell's height to accommodate the height of the image.如果要在保持纵横比的同时以全宽显示完整图像,则必须增加该单元格的高度以适应图像的高度。

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

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