How to manage UIImageView content mode?

83,710

Solution 1

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

Solution 2

You just need these two lines:

imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.layer.clipsToBounds = YES; // Must do this or else image will overflow

Solution 3

Here a solution for swift:

let imageView = UIImageView(image: UIImage(named: "backgroundImage"))
imageView.frame = tableView.frame
imageView.contentMode = .ScaleAspectFill
tableView.backgroundView = imageView

Solution 4

In swift you set content mode as well

var newImgThumb : UIImageView
newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70))
newImgThumb.contentMode = .ScaleAspectFit

Solution 5

Worth noting that swift 3 has changed as following

.ScaleAspectFit to scaleAspectFit

.ScaleAspectFill to .scaleAspectFill

Share:
83,710

Related videos on Youtube

DipakSonara
Author by

DipakSonara

I am an iOS developer.

Updated on July 09, 2022

Comments

  • DipakSonara
    DipakSonara almost 2 years

    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 ?

    • Midhun MP
      Midhun MP over 11 years
      Why don't you check by using the above mentioned values ? You can find the difference easily
    • Anusha Kottiyal
      Anusha Kottiyal over 11 years
    • fibnochi
      fibnochi over 11 years
      UIViewContentModeScaleAspectFill use tho property of UIImageView content mode. It will fit your image to UIIMageView rect size.
  • DipakSonara
    DipakSonara over 11 years
    No, its not working. Image isn't covering whole area of UIImageView.
  • NiravPatel
    NiravPatel over 11 years
    yes, it will not cover the whole area of UIImageview because it will set your image according to ratio, so it will left some area blank according to ratio.so you will get image without blur and get clear image.so do you want to cover the whole area of UIImageview?
  • birdman
    birdman about 11 years
    This answer deserves many more upvotes!! Helped me when moving and sizing/scaling images within UIImageViews around the view. Now my frame.origin.x, frame.origin.y, frame.size.height and frame.size.width positions are where they should be.
  • helsont
    helsont almost 9 years
    Update: UIViewContentMode.ScaleAspectFill, and UIViewAutoresizing.FlexibleBottomMargin
  • lppier
    lppier over 8 years
    I love you. I can go to bed now.
  • halbano
    halbano over 8 years
    There's a way to do this using constraints instead of masks? Or where can I set the masks on the storyboard having constraints enabled?
  • maddy
    maddy about 8 years
    i also put clipsToBound = YES;
  • StoriKnow
    StoriKnow over 5 years
    Thank you! ClipsToBounds was my issue. I had it set to NO so I could display a shadow around the UIImageView, without thinking about the fact that it'll also let the image bleed over. Thanks for the tip.