Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image is not fit to the frame of UIImageView

I am creating the UIImageView using interface Builder. Its frame size is (320,67). I want to display the image on the imageView. I am getting the image from the web. The problem is that the image get from web is stretched to display on the imageview... Here my code

NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.isco.com/webproductimages/appBnr/bnr1.jpg"]];
imageView.image = [UIImage imageWithData:imageData];

Can anyone tel me that how to display the image fit to display on imageView????

like image 635
Venk Avatar asked Sep 15 '12 09:09

Venk


2 Answers

Either use

imageView.contentMode = UIViewContentModeScaleAspectFill;

or

imageView.contentMode = UIViewContentModeScaleAspectFit;

The first one will fill the frame, possibly cutting off parts of your image. The second one will show the entire image, possibly leaving areas of the frame open.

like image 198
mvds Avatar answered Sep 30 '22 00:09

mvds


For Swift :

imageView.contentMode = UIViewContentMode.ScaleAspectFit

For Objective-C :

 imageView.contentMode = UIViewContentModeScaleAspectFit

ScaleAspectFit option is use to scale the content to fit the size of the view by maintaining the aspect ratio. Any remaining areas of the view’s bounds is transparent.

Screenshot

Refer Apple docs for details.

like image 26
Jayprakash Dubey Avatar answered Sep 30 '22 00:09

Jayprakash Dubey