简体   繁体   中英

Resize UIImageView according to image size?

In IB we can use command = to resize the UIImageView to the original size of image. I need to do it programmatically.I used contentMode but it resize image itself to match UIImageView size set in IB. Edit: I've tried

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

But it is not working.What i'm doing wring?

verifyCell.scanStatusImage.contentMode = UIViewContentModeScaleAspectFit;

verifyCell.scanStatusImage .clipsToBounds = YES;

[verifyCell.scanStatusImage sizeToFit];

Hope This Helps You......!

it would be something like this: get image size by:

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

calculate scale ratio on width

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

check scaled height

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

SizeToFit方法将完成此工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM