Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage UIImageView content mode?

I have an UIImageView with some fixed rect size. But my images are not fixed in size. I want to display images according to UIImageView's rect size. Whether image is big or large in resolution it must be in fixed display according to UIImageView's size. I am confused in using below assets.

UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, UIViewContentModeScaleAspectFill, UIViewContentModeRedraw 

What to set in autoresize mask ? How do they behave ?

like image 982
DipakSonara Avatar asked Jan 03 '13 06:01

DipakSonara


People also ask

What is content mode?

The content mode specifies how the cached bitmap of the view's layer is adjusted when the view's bounds change. This property is often used to implement resizable controls.

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 .

How do I add images to UIImageView?

With Interface Builder it's pretty easy to add and configure a UIImageView. The first step is to drag the UIImageView onto your view. Then open the UIImageView properties pane and select the image asset (assuming you have some images in your project).

What is UIImageView in Swift?

A view that displays a single image or a sequence of animated images in your interface.


2 Answers

Please try this code, Hope it will work for you.

set UIImageView contentMode to UIViewContentModeScaleAspectFill as below :

imageView.contentMode = UIViewContentModeScaleAspectFill; 

set autoresizingMask of UIImageView as below :

imageView.autoresizingMask =        ( UIViewAutoresizingFlexibleBottomMargin     | UIViewAutoresizingFlexibleHeight     | UIViewAutoresizingFlexibleLeftMargin     | UIViewAutoresizingFlexibleRightMargin     | UIViewAutoresizingFlexibleTopMargin     | UIViewAutoresizingFlexibleWidth ); 
like image 83
sagarcool89 Avatar answered Oct 09 '22 06:10

sagarcool89


You just need these two lines:

imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.layer.clipsToBounds = YES; // Must do this or else image will overflow 
like image 34
atulkhatri Avatar answered Oct 09 '22 06:10

atulkhatri