How to manage UIImageView content mode?

DipakSonara picture DipakSonara · Jan 3, 2013 · Viewed 80.9k times · Source

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 ?

Answer

sagarcool89 picture sagarcool89 · Jan 3, 2013

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 );