![](/img/trans.png)
[英]Set UIImageVIew frame according to Image's Frame contain By UIImageView
[英]Image is not fit to the frame of UIImageView
我正在使用界面生成器創建UIImageView
。 其框架尺寸為(320,67)。 我想在imageView上顯示圖像。 我從網上獲取圖像。 問題是從web獲取的圖像被拉伸到imageview上顯示...這是我的代碼
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.isco.com/webproductimages/appBnr/bnr1.jpg"]];
imageView.image = [UIImage imageWithData:imageData];
誰能告訴我如何顯示圖像適合在imageView上顯示?
要么使用
imageView.contentMode = UIViewContentModeScaleAspectFill;
要么
imageView.contentMode = UIViewContentModeScaleAspectFit;
第一個將填充框架,可能會切斷圖像的一部分。 第二個將顯示整個圖像,可能會使框架的區域打開。
對於Swift:
imageView.contentMode = UIViewContentMode.ScaleAspectFit
對於Objective-C:
imageView.contentMode = UIViewContentModeScaleAspectFit
ScaleAspectFit選項用於通過保持縱橫比來縮放內容以適合視圖的大小。 視圖邊界的任何剩余區域都是透明的。
有關詳細信息,請參閱Apple文檔 。
在根據圖像的縱橫比縮放圖像后,我最終調整了圖像的大小。
let widthRatio = ImageView.bounds.size.width / (captureImageView.image?.size.width)!
let heightRatio = ImageView.bounds.size.height / (captureImageView.image?.size.height)!
let scale = min(widthRatio, heightRatio)
let imageWidth = scale * (ImageView.image?.size.width)!
let imageHeight = scale * (ImageView.image?.size.height)!
ImageView.frame = CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight)
試試這個代碼。
imView.contentMode = UIViewContentModeScaleAspectFit;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.