UIImageView aspect fit and center UIImageView aspect fit and center ios ios

UIImageView aspect fit and center


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


In swift language we can set content mode of UIImage view like following as:

let newImgThumb = UIImageView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))newImgThumb.contentMode = .scaleAspectFit


Swift

yourImageView.contentMode = .center

You can use the following options to position your image:

  • scaleToFill
  • scaleAspectFit // contents scaled to fit with fixedaspect. remainder is transparent
  • redraw // redraw on bounds change (calls -setNeedsDisplay)
  • center // contents remain same size. positioned adjusted.
  • top
  • bottom
  • left
  • right
  • topLeft
  • topRight
  • bottomLeft
  • bottomRight