RockBaby RockBaby - 3 months ago 17
iOS Question

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.

Answer

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