Alexander Perechnev
Alexander Perechnev

Reputation: 2837

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.

Upvotes: 2

Views: 10913

Answers (1)

Basheer_CAD
Basheer_CAD

Reputation: 4919

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;

Upvotes: 7

Related Questions