Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling UIImageView to fit screen width

I have an UIImageView created using Storyboard inside UIViewController.

I'm trying to rescale the width of the image to fit the screen width and rescale 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;

It then shows an image which is scaled but it fits some other area and there is a whitespace aruond my image.

like image 817
syntagma Avatar asked Sep 13 '12 10:09

syntagma


People also ask

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.

What is UIImage?

An object that manages image data in your app.


1 Answers

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;
like image 128
sergio Avatar answered Nov 15 '22 20:11

sergio