1

I have an app with images displayed in it. I have various images at different sizes displayed in my UIImageView.

What I want to do is set a size for the image view - call it 100x100.

  • If an image with a size overall less than 100x100 is put in the image view, I want the image view to center the image.
  • If an image that is overall bigger than the view, e.g. 200x300, then I want the image view to center the image and scale it down until it just fits within the bounds of the image view.

Is there a way to do this in a normal UIImageView? Do I need to do some magic somewhere?

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
Undo
  • 25,519
  • 37
  • 106
  • 129

3 Answers3

2

You can do this with a normal UIImage view. For the larger image, you want to set the image view's Mode in IB to Aspect Fit or Scale To Fill (depending on whether you want the aspect ratio to stay the same or not), and for the smaller image, set it to Center.

You can do the same thing in code by using the contentMode property of the imageView.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
1

If the image is smaller then add the UIImageView to a new transparent view and center it in that view. If larger then decide how you want the view resized (look at the resize option in UIView), and use just that UIImageView.

David H
  • 40,852
  • 12
  • 92
  • 138
  • 1
    I'm answering with my wife's iPad. Centering a view in another view is trivial,no? You will see the resize options in the UIView class ref, I forget the property name offhand. If you really need more help/code can do tomorrow AM NYC time. – David H Apr 29 '13 at 00:40
1

This answer helped me in the end. It had this code:

- (CGSize) aspectScaledImageSizeForImageView:(UIImageView *)iv image:(UIImage *)im {

     float x,y;
     float a,b;
     x = iv.frame.size.width;
     y = iv.frame.size.height;
     a = im.size.width;
     b = im.size.height;

     if ( x == a && y == b ) {           // image fits exactly, no scaling required
         // return iv.frame.size;
     }
     else if ( x > a && y > b ) {         // image fits completely within the imageview frame
         if ( x-a > y-b ) {              // image height is limiting factor, scale by height
             a = y/b * a;
             b = y;
         } else {
             b = x/a * b;                // image width is limiting factor, scale by width
             a = x;
         }
     } 
     else if ( x < a && y < b ) {        // image is wider and taller than image view
         if ( a - x > b - y ) {          // height is limiting factor, scale by height
             a = y/b * a;
             b = y;
         } else {                        // width is limiting factor, scale by width
             b = x/a * b;
             a = x;
         }
     }
    else if ( x < a && y > b ) {        // image is wider than view, scale by width
        b = x/a * b;
        a = x;
    }
    else if ( x > a && y < b ) {        // image is taller than view, scale by height
        a = y/b * a;
        b = y;
    }
    else if ( x == a ) {
    a = y/b * a;
    b = y;
    } else if ( y == b ) {
        b = x/a * b;
        a = x;
    }
    return CGSizeMake(a,b);
}
Community
  • 1
  • 1
Undo
  • 25,519
  • 37
  • 106
  • 129
  • This is a lot of code, instead you could just: if(myImage.size > 200x300) myImageView.contentMode = UIViewContentModeScaleAspectFit; – David H Apr 29 '13 at 12:23
  • You need to round or truncate a and b so they are both integral numbers. – David H Jun 04 '13 at 19:41
  • @DavidH It worked fine for my purposes, feel free to make any changes necessary. – Undo Jun 04 '13 at 19:43