Scaling UIImageView to fit the width of the screen

I have a UIImageView created using a Storyboard inside a UIViewController .

I am trying to scale the width of the image to fit the width of the screen and scale the height proportionally:

 [fImage setImage:[UIImage imageNamed: @"f2345.jpeg"]]; [fImage sizeToFit]; fImage.contentMode = UIViewContentModeScaleAspectFit; CGRect frame = filmImage.frame; frame.size.width = [[UIScreen mainScreen] bounds].size.width; // Alternative way to do the similar thing - gives the same result in my case // frame.size.width = [[UIScreen mainScreen] applicationFrame].size.width; fImage.frame = frame; 

Then it shows an image that is scaled, but it is suitable for some other area, and there is a space in my image.

+7
source share
1 answer

Try using this code:

 float imgFactor = frame.size.height / frame.size.width; frame.size.width = [[UIScreen mainScreen] bounds].size.width; frame.size.height = frame.size.width * imgFactor; 
+12
source

All Articles