简体   繁体   中英

How to set size of UIImageView equal to size of UIImage?

I have an view controller with UIImageView named someImageView . This image view has no default UIImage . I'm loading UIImage with the code below:

UIImage * myImage = [UIImage imageNamed:@"sampleImage.png"];
someImageView.image = myImage;

The size of sampleImage.png is 320x300. I need to set size of someImageView to size exactly equal to sampleImage.png . I'm trying to do it:

CGRect frame = someImageView.frame;
frame.size = myImage.size;
someImageView.frame = frame;

But it doesn't work.

You have to change contentMode to UIViewContentModeScaleToFill of your imageView

someImageView.image = myImage;
someImageView.contentMode = UIViewContentModeScaleToFill;

CGRect frame = someImageView.frame;
frame.size = myImage.size;
someImageView.frame = frame;

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