簡體   English   中英

根據圖像大小調整UIImageView的大小?

[英]Resize UIImageView according to image size?

在IB中,我們可以使用command =UIImageView調整為原始圖像大小。 我需要以編程方式進行操作。我使用了contentMode但是它會調整圖像本身的大小以匹配IB中設置的UIImageView大小。 編輯:我已經嘗試過

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ScanToVerifyCell" forIndexPath:indexPath];
            STOScanToVerifyCell * verifyCell = (STOScanToVerifyCell *)cell;
            [verifyCell.scanStatusImage setHidden:NO];
            [verifyCell.scanStatusImage setImage:[UIImage imageNamed:@"point.png"]];
            [verifyCell.scanStatusImage sizeToFit];

但這不起作用。我在干什么?

verifyCell.scanStatusImage.contentMode = UIViewContentModeScaleAspectFit;

verifyCell.scanStatusImage .clipsToBounds = YES;

[verifyCell.scanStatusImage sizeToFit];

希望對您有幫助...!

就像這樣:通過以下方式獲取圖像大小:

UIImage * img = [UIImage imageNamed:@"someImage.png"];
CGSize imgSize = img.size;

計算寬度比例

float ratio=yourImageView.frame.size.width/imgSize.width;

檢查縮放高度

float scaledHeight=imgSize.height*ratio;
if(scaledHeight < yourImageView.frame.size.height)
{
   //update height of your imageView frame with scaledHeight
}

SizeToFit方法將完成此工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM