本文介绍了即使 contentMode = UIViewContentModeScaleAspectFit UIImageView 图像也会拉伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 UIImageView 显示图像的方式感到非常失望.

我有以下一段代码:

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];

我得到的是以下屏幕截图:

http://img26.imageshack.us/img26/3686/screenshot20130123at629.png>

[图像被拉伸到 UIImageView 的全宽,甚至没有全高]

原图:

http://img248.imageshack.us/img248/341/screenshot20130123at638.png>

解决方案

设置大小必须在设置 contentMode 之后进行

[imageView setContentMode: UIViewContentModeScaleAspectFit];imageView.frame = CGRectMake(x, y, width, height);

I am really disappointed by the way UIImageView displays the image.

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

[image is stretched to full width of UIImageView and does not even take full height]

Original image:

http://img248.imageshack.us/img248/341/screenshot20130123at638.png

解决方案

Setting the size must occur AFTER setting the contentMode

[imageView setContentMode: UIViewContentModeScaleAspectFit];
imageView.frame = CGRectMake(x, y, width, height);

这篇关于即使 contentMode = UIViewContentModeScaleAspectFit UIImageView 图像也会拉伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:40