图像被拉伸

时间:2012-05-09 16:39:12

标签: objective-c ios uiimageview

我有一个图像(从我们的服务器获取)我想放入UIImageView。图像是373x500。 UIImageView是443x290,视图正在拉伸它。

UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 
                                                                         0, 
                                                                         500, 
                                                                         300)];

myImageView.image = [UIImage imageWithData:photoData];
myImageView.backgroundColor = [UIColor viewBackgroundColor];
myImageView.contentMode = UIViewContentModeScaleAspectFit;

NSLog(@"myImageView.image.size: %@", NSStringFromCGSize(myImageView.image.size));
NSLog(@"myImageView.frame.size: %@", NSStringFromCGSize(myImageView.frame.size));

[wrapperView addSubview:myImageView];

日志读取:

2012-05-09 11:32:52.621[89145:1a303] myImageView.image.size: {373, 500}
2012-05-09 11:32:52.622[89145:1a303] myImageView.frame.size: {443, 290}

看起来像:

enter image description here

不应该“UIViewContentModeScaleAspectFit”告诉它保持比例而不是拉伸图像?

3 个答案:

答案 0 :(得分:1)

您也应该在视图上设置clipsToBounds = YES

答案 1 :(得分:0)

UIViewContentModeScaleAspectFit缩放图片(顾名思义)以适合您的视图的宽高比。如果您不希望它缩放图像,请使用UIViewContentModeCenter,它只会将图像以原始大小放置在视图的中心。

答案 2 :(得分:0)

为我解决的是在设置框架之前设置内容模式...

UIImageView *imageView = [UIImageView new];
imageView.myImage;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.frame = CGRectMake(...);
[self.view addSubiew:imageView];