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
Related videos on Youtube
Comments
-
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 over 11 yearsWhy don't you check by using the above mentioned values ? You can find the difference easily
-
Anusha Kottiyal over 11 yearsSee this link : stackoverflow.com/questions/4895272/…
-
fibnochi over 11 yearsUIViewContentModeScaleAspectFill use tho property of UIImageView content mode. It will fit your image to UIIMageView rect size.
-
-
DipakSonara over 11 yearsNo, its not working. Image isn't covering whole area of UIImageView.
-
NiravPatel over 11 yearsyes, 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 about 11 yearsThis 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 almost 9 yearsUpdate: UIViewContentMode.ScaleAspectFill, and UIViewAutoresizing.FlexibleBottomMargin
-
lppier over 8 yearsI love you. I can go to bed now.
-
halbano over 8 yearsThere'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 about 8 yearsi also put clipsToBound = YES;
-
StoriKnow over 5 yearsThank you!
ClipsToBounds
was my issue. I had it set toNO
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.