[英]UIImageView image stretched even though contentMode = UIViewContentModeScaleAspectFit
I am really disappointed by the way UIImageView displays the image. UIImageView显示图像的方式让我真的很失望。
I have following piece of code: 我有以下代码:
UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];
imgview.image = [UIImage imageNamed:@"img.JPG"];
imgview.backgroundColor = [UIColor greenColor];
imgview.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imgview];
What I get is a following screenshot: 我得到的是以下屏幕截图:
http://img26.imageshack.us/img26/3686/screenshot20130123at629.png http://img26.imageshack.us/img26/3686/screenshot20130123at629.png
[image is stretched to full width of UIImageView and does not even take full height] [图像被拉伸到UIImageView的全宽,甚至没有全高]
Original image: 原始图片:
http://img248.imageshack.us/img248/341/screenshot20130123at638.png http://img248.imageshack.us/img248/341/screenshot20130123at638.png
Setting the size must occur AFTER setting the contentMode 设置大小必须在设置contentMode之后发生
[imageView setContentMode: UIViewContentModeScaleAspectFit];
imageView.frame = CGRectMake(x, y, width, height);
I had the same problem. 我有同样的问题。 In my project a
UIImage
created with imageWithCIImage:
never respected the contentMode
of the UIImageView
it was inserted into. 在我的项目中,使用
imageWithCIImage:
创建的UIImage
从未考虑将UIImageView
插入到其中的contentMode
。
Some images do also have weird attributes that causes the same problem. 一些图像也确实具有导致相同问题的怪异属性。 Converting it to PNG with Photoshop always worked on them.
使用Photoshop将其转换为PNG始终可以解决问题。
Not working with contentMode
: 不工作
contentMode
:
CIImage * __strong ciImage = [CIImage imageWithContentsOfURL:url];
_image = [UIImage imageWithCIImage:ciImage];
The UIImageView
seems to resize based on the CGImage
. UIImageView
似乎根据CGImage
调整大小。
You need to render the CIImage to a CGImage and can than use that as an UIImage. 您需要将CIImage渲染为CGImage,然后可以将其用作UIImage。 This will work with
UIViewContentModeAspectFit
. 这将与
UIViewContentModeAspectFit
。
Use for example createCGImage:fromRect:
, or better use a CGImage
from the start. 例如使用
createCGImage:fromRect:
或者最好从一开始就使用CGImage
。
I just ran it and your code works fine. 我刚运行它,您的代码运行正常。 It could have something to do with you specifying JPG when its actually a png?
它可能与您在实际指定png时指定JPG有关?
You can also use UIViewContentModeScaleAspectFill, but make sure that your bounds are within the view limit because otherwise the outside might be clipped 您也可以使用UIViewContentModeScaleAspectFill,但请确保边界在视图限制之内,否则可能会限制外部区域
This is a dumb one, so stupid I'm proud of it. 这是愚蠢的,我为之愚蠢而感到自豪。 Bottom line is I ended up on this SO post thinking contentMode = UIViewContentModeScaleAspectFit was broken when in fact what was really broken was my expectation of the outcome.
最重要的是,我结束了这篇文章,以为contentMode = UIViewContentModeScaleAspectFit被打破了,而实际上真正打破的是我对结果的期望。 I was working with a square image in a square view so Fit and Fill were having the exact same effect.
我正在以正方形视图处理正方形图像,因此Fit和Fill具有完全相同的效果。 I cried like a baby when I couldn't get it to sit widescreen inside the square.
当我不能让它宽屏坐在广场内时,我像婴儿一样哭了。
In my case, i solved this removing some constraints that resize the imageview that i set by error. 就我而言,我解决了此问题,消除了一些限制 ,这些限制会调整我错误设置的imageview的大小。
[UPDATE] [更新]
It is "Size Constraints" the one that make size relative to another view. 是“尺寸约束”使尺寸相对于另一个视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.