What is equivalent to center crop scale type of Android ImageView for iOS UIImageView?
Asked Answered
F

3

6

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.

Furmenty answered 7/2, 2019 at 6:29 Comment(4)
#14134535Movement
Set imageView.contentMode to .scaleAspectFill (or .scaleAspectFit).Misspend
@Misspend scaleAspectFit is equivalent of android centerInsideMummify
@iffatfatima - Yep, I just wanted to share both of the aspect preserving scaling contentModes.Misspend
F
11

You should use .scaleAspectFill. In swift 4.2:

myImageView.contentMode = .scaleAspectFill
myImageView.clipsToBounds = true // If you want to crop
Farce answered 7/2, 2019 at 6:38 Comment(2)
.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
M
0

You can set image view contentMode to UIView.ContentMode.scaleAspectFill. It is equivalent of Android scaleType centerCrop

Mummify answered 7/2, 2019 at 6:37 Comment(0)
M
0

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.

Melvinamelvyn answered 7/2, 2019 at 6:39 Comment(2)
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.