I would like to ask how to scale an image to fill a UIImageView
but still keep its aspect ratio, just like the centerCrop
scale type of Android ImageView
does.
What is equivalent to center crop scale type of Android ImageView for iOS UIImageView?
Asked Answered
You should use .scaleAspectFill
. In swift 4.2:
myImageView.contentMode = .scaleAspectFill
myImageView.clipsToBounds = true // If you want to crop
.scaleAspectFill will stretch the image to fit into UIImageView. If you want to keep the aspect ratio, you should use .scaleAspectFit. The best way is you try both solution in the code to see the final result. If this helps you, please upvote my post. Thank you! –
Farce
Yes, you're right @Rob. I'll update my answer. Thank you. –
Farce
You can set image view contentMode
to UIView.ContentMode.scaleAspectFill
. It is equivalent of Android scaleType
centerCrop
Following are the main content modes of UIImageView:
- UIViewContentModeScaleToFill
- UIViewContentModeScaleAspectFit
- UIViewContentModeScaleAspectFill
the one to fill the whole imageview is 'ScaleAspectFill' plus you can also use 'clipsToBound' property as true so that it will crop the extra image which is out of imageview.
Those are the Objective-C constants, but OP was asking about Swift. –
Misspend
he can write like imageView.contentMode = .scaleAspectFill & if he want to set the image as its pixels quality .scaleAspectFit will be perfect. –
Melvinamelvyn
© 2022 - 2024 — McMap. All rights reserved.
imageView.contentMode
to.scaleAspectFill
(or.scaleAspectFit
). – MisspendscaleAspectFit
is equivalent of androidcenterInside
– MummifycontentMode
s. – Misspend