UIImageView aspect fit and center

311,914

Solution 1

Just pasting the solution:

Just like @manohar said

imageView.contentMode = UIViewContentModeCenter;
if (imageView.bounds.size.width > ((UIImage*)imagesArray[i]).size.width && imageView.bounds.size.height > ((UIImage*)imagesArray[i]).size.height) {
       imageView.contentMode = UIViewContentModeScaleAspectFit;
}

solved my problem

Solution 2

In swift language we can set content mode of UIImage view like following as:

let newImgThumb = UIImageView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
newImgThumb.contentMode = .scaleAspectFit

Solution 3

Swift

yourImageView.contentMode = .center

You can use the following options to position your image:

  • scaleToFill
  • scaleAspectFit // contents scaled to fit with fixed aspect. remainder is transparent
  • redraw // redraw on bounds change (calls -setNeedsDisplay)
  • center // contents remain same size. positioned adjusted.
  • top
  • bottom
  • left
  • right
  • topLeft
  • topRight
  • bottomLeft
  • bottomRight

Solution 4

Updated answer

When I originally answered this question in 2014, there was no requirement to not scale the image up in the case of a small image. (The question was edited in 2015.) If you have such a requirement, you will indeed need to compare the image's size to that of the imageView and use either UIViewContentModeCenter (in the case of an image smaller than the imageView) or UIViewContentModeScaleAspectFit in all other cases.

Original answer

Setting the imageView's contentMode to UIViewContentModeScaleAspectFit was enough for me. It seems to center the images as well. I'm not sure why others are using logic based on the image. See also this question: iOS aspect fit and center

Solution 5

I solved this problem like this.

  1. setImage to UIImageView (with UIViewContentModeScaleAspectFit)
  2. get imageSize (CGSize imageSize = imageView.image.size)
  3. UIImageView resize. [imageView sizeThatFits:imageSize]
  4. move position where you want.

I wanted to put UIView on the top center of UICollectionViewCell. so, I used this function.

- (void)setImageToCenter:(UIImageView *)imageView
{
    CGSize imageSize = imageView.image.size;
    [imageView sizeThatFits:imageSize];
    CGPoint imageViewCenter = imageView.center;
     imageViewCenter.x = CGRectGetMidX(self.contentView.frame);
    [imageView setCenter:imageViewCenter];
}

It works for me.

Share:
311,914
Ravi Vooda
Author by

Ravi Vooda

Work my way up! Coder from CodeLand.

Updated on July 16, 2022

Comments

  • Ravi Vooda
    Ravi Vooda almost 2 years

    I have an image view, declared programmatically, and I am setting its image, also programmatically.

    However, I find myself unable to set the image to both fit the aspect and align centre to the image view.

    In other words, I want the image to:

    • Scale down to fit the aspect, if the image is large.
    • Centre but not scale up, if the image is small.

    How do I get that?

    • Manu
      Manu about 11 years
      [yourImageView setContentMode:UIViewContentModeCenter]; and make sure if imageView Frame is greater than image frame. if it's not then place [yourImageView setContentMode:UIViewContentModeAspectToFit];
    • Manu
      Manu about 11 years
      if else condition dude @RyanPoolos
  • Skrew
    Skrew over 10 years
    This will ignore the scaling
  • Cfr
    Cfr over 9 years
    Shorter way to find out image is smaller: CGRectContainsRect(imageView.bounds, {CGPointZero, image.size})
  • Timur Bernikovich
    Timur Bernikovich over 7 years
    @Cfr maybe it's shorter, but not so readable. At the same time I propose to split line with if-statement like this: CGSize imageSize = ((UIImage*)imagesArray[i]).size;, CGSize viewSize = imageView.bounds.size, if (viewSize.width > imageSize.width && viewSize.height > imageSize.height) {.
  • NoelHunter
    NoelHunter about 7 years
    Should be UIViewContentModeScaleAspectFit
  • Arjay Waran
    Arjay Waran almost 7 years
    imageView.contentMode = UIViewContentModeScaleAspectFit;
  • slow
    slow about 6 years
    I don't know why solutions like this got so many upvotes. This person clearly didn't read the question at all.
  • fishinear
    fishinear over 5 years
    This blows up the image if it is smaller than the image view, contrary to what the OP asked for.
  • fishinear
    fishinear over 5 years
    This blows up the image if it is smaller than the image view, contrary to what the OP asked for.
  • fishinear
    fishinear over 5 years
    This blows up the image if it is smaller than the image view, contrary to what the OP asked for.
  • Nate Cook
    Nate Cook over 5 years
    @fishinear The requirement about not scaling the image up was added to the question a year after I answered it: stackoverflow.com/posts/15499376/revisions