根据图像大小调整UIImageView的大小?

时间:2013-11-20 07:21:56

标签: ios objective-c

在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];

但它不起作用。我在做什么呢?

3 个答案:

答案 0 :(得分:4)

verifyCell.scanStatusImage.contentMode = UIViewContentModeScaleAspectFit;

verifyCell.scanStatusImage .clipsToBounds = YES;

[verifyCell.scanStatusImage sizeToFit];

希望这有助于你......!

答案 1 :(得分:2)

它将是这样的:通过以下方式获取图像大小:

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
}

答案 2 :(得分:0)

SizeToFit方法将完成这项工作。