简体   繁体   English

即使contentMode = UIViewContentModeScaleAspectFit,UIImageView图像也会拉伸

[英]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. 是“尺寸约束”使尺寸相对于另一个视图。

Xamarin IOS Doc Xamarin IOS文件

暂无
暂无

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

相关问题 当contentMode UIViewContentModeScaleAspectFit时,禁用UIImageView调整大小的行为 - Disable UIImageView resizing behavior when contentMode UIViewContentModeScaleAspectFit 如何避免使用UIImageView的contentmode UIViewContentModeScaleAspectFit保持高度恒定而损失图像质量? - How to avoid losing image quality using UIImageView's contentmode UIViewContentModeScaleAspectFit keeping height constant? 在将contentMode设置为UIViewContentModeScaleAspectFit时,如何设置UIImageView左对齐或右对齐? - how to set the UIImageView align left or right when set the contentMode to UIViewContentModeScaleAspectFit? 使用UIViewContentModeScaleAspectFit在UIImageView中调整图像 - Fit image in UIImageView using UIViewContentModeScaleAspectFit UICollectionViewCell UIImageView 忽略 contentMode,溢出图像 - UICollectionViewCell UIImageView ignores contentMode, overflows image UIImageView:结合UIViewContentModeScaleAspectFit和UIViewContentModeBottom - UIImageView: combine UIViewContentModeScaleAspectFit and UIViewContentModeBottom 设置内容模式后,UIImageView仍会拉伸 - UIImageView stretched even after setting content mode UIImageView框架是正确的,contentMode是正确的,图像无法正确显示 - UIImageView frame is right, contentMode is right, image not displayed correctly 在运行时设置图像时不遵守UIImageView的contentmode - UIImageView's contentmode not respected when setting image during runtime UIImageView contentMode Scale Aspect Fill 正在拉伸图像(没关系,它不是) - UIImageView contentMode Scale Aspect Fill is Stretching the Image (Nevermind, It Wasn't)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM