Question

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.

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top