2014-03-06 48 views
0

我的形象图我初始化这样的:如何显示填充屏幕宽度的UIImageView,并设置高度以保持宽高比?

self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)]; 
[self.imageView setContentMode:UIViewContentModeScaleAspectFill]; 
self.imageView.clipsToBounds = YES; 

我再取网页上的图像,并将其放置在视图中。我的问题是,我实际上想显示完整的图像,而不是裁剪它,所以它适合200像素。

我该怎么做?谢谢!

回答

2

所以你检查图像和计算宽度基于图像的尺寸:

UIImage *downloadedImage; // assuming this is not nil 
CGFloat imageRatio = downloadedImage.size.height/downloadedImage.size.width; 
self.imageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * imageRatio); 

有一个更好的方式来获得屏幕宽度比使用宏的方式(CGRectGetWidth(self.view.bounds)),但是这不是与你的问题有关。

1

使用内容模式UIViewContentModeScaleAspectFit缩放图像以适合图像视图,保持图像的宽高比。

你的问题标题不符合你的问题的身体,所以我用身体去:

我的问题是,其实我是想表现完整的图像,而不是 作物,以便它适合200像素。