![](/img/trans.png)
[英]How to display a UIImageView that fills the width of the screen, and sets height to keep aspect ratio?
[英]UIImageView with fixed width: how to set its height to keep its aspect?
我有一個視圖,我需要放置一個UIImageView
,我被告知將其放在一個矩形內,該矩形占用屏幕寬度,其高度小於寬度(固定大小)。 然而,我給出的圖像或多或少是方形jpeg
圖像,因此想法是填充應包含圖像的矩形的寬度,並以保持其縱橫比的方式設置圖像的高度。 然后,我應該能夠讓用戶垂直滾動圖像以查看完整的圖像。
我怎么能這樣做?
提前致謝
編輯:我需要為具有不同大小的幾個圖像執行此操作,但這應該在視圖中適合相同的矩形區域大小
您可以設置imageview內容模式。
imageView.contentMode = UIViewContentModeScaleAspectFit;
這將確保通過保持原始寬高比來顯示圖像。
編輯:
我想這就是你想要的:
UIImage *originalImage = [UIImage imageNamed:[picArr objectAtIndex:a]];
double width = originalImage.size.width;
double height = originalImage.size.height;
double apect = width/height;
double nHeight = 320.f/ apect;
self.img.frame = CGRectMake(0, 0, 320, nHeight);
self.img.center = self.view.center;
self.img.image = originalImage;
希望這可以幫助.. :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.