Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView aspect fit and center

I have an image view, declared programmatically, and I am setting its image, also programmatically.

However, I find myself unable to set the image to both fit the aspect and align centre to the image view.

In other words, I want the image to:

  • Scale down to fit the aspect, if the image is large.
  • Centre but not scale up, if the image is small.

How do I get that?

like image 423
Ravi Vooda Avatar asked Mar 19 '13 12:03

Ravi Vooda


1 Answers

Just pasting the solution:

Just like @manohar said

imageView.contentMode = UIViewContentModeCenter; if (imageView.bounds.size.width > ((UIImage*)imagesArray[i]).size.width && imageView.bounds.size.height > ((UIImage*)imagesArray[i]).size.height) {        imageView.contentMode = UIViewContentModeScaleAspectFit; } 

solved my problem

like image 197
Ravi Vooda Avatar answered Sep 28 '22 03:09

Ravi Vooda