Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView fill in UIView

I have a UIImageView as a subview of UIView (frame size 300 x 300). My UIImage would be either portrait, landscape or some odd sizes. How do I fill in the UIImage within the UIView frame size without stretching the image to 300 x 300?

The purpose of this, I will be adding another UIImageView as the image border. Please help.

Thanks.

like image 361
RockBaby Avatar asked Mar 01 '12 18:03

RockBaby


2 Answers

Try

[img setContentMode:UIViewContentModeScaleAspectFit];

UIViewContentModeScaleAspectFit: Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.

or [img setContentMode:UIViewContentModeScaleAspectFill];

UIViewContentModeScaleAspectFill: Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.

if you are clipping subviews, then you need to do

[imgView clipToBounds:YES];
like image 135
Srikar Appalaraju Avatar answered Sep 20 '22 17:09

Srikar Appalaraju


Ah I just reread what you were asking, I know what your question is now.

Your UIImageView's frame changes, and when it does so does your image. You don't want your image to change, but you do want your ImageView to adjust to fill the view it is contained in.

UPDATE

I figured it out.

[self.imageView setContentMode:UIViewContentModeCenter];

No matter what orientation, the size stays the same.

You also have the option of setting it to align top, bottom, left, right, top left, top right, bottom left, bottom right, all of which only help to align your image and NOT redraw or re-size the image.

Does that help?

like image 31
Fang Chen Avatar answered Sep 22 '22 17:09

Fang Chen