-1

I have to tried all type of content mode, Third party and lots of googling. But, not success for me. I want to show like Facebook image post. We are post image in facebook, facebook show properly images and fill the image very properly into image view.

Nikunj Gangani
  • 224
  • 2
  • 10

2 Answers2

0

Follow steps

  1. setImage to UIImageView (with UIViewContentModeScaleAspectFit)
  2. get imageSize (CGSize imageSize = imageView.image.size)
  3. UIImageView resize. [imageView sizeThatFits:imageSize]
  4. move position where you want.

        CGSize imageSize = imageView.image.size;
        [imageView sizeThatFits:imageSize];
        CGPoint imageViewCenter = imageView.center;
         imageViewCenter.x = CGRectGetMidX(self.contentView.frame);
        [imageView setCenter:imageViewCenter];
    
junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
  • 1
    Thanks @junaidsidhu. I want to fill the image like Facebook.Any type of image(like height > width, width(height*2)>height) fill very properly by facebook. – Nikunj Gangani Jul 10 '17 at 07:39
0

set UIImageView size == image (ratio):

@IBOutlet weak var heightConstraint: NSLayoutConstraint!
@IBOutlet weak var widthConstraint: NSLayoutConstraint!

func setImage(image: UIImage) {
    imageView.image = image
    let screenSize = UIScreen.main.bounds.size

    let imageAspectRatio = image.size.width / image.size.height
    let screenAspectRatio = screenSize.width / screenSize.height

    if imageAspectRatio > screenAspectRatio {
        widthConstraint.constant = min(image.size.width, screenSize.width)
        heightConstraint.constant = widthConstraint.constant / imageAspectRatio
    }
    else {
        heightConstraint.constant = min(image.size.height, screenSize.height)
        widthConstraint.constant = heightConstraint.constant * imageAspectRatio
    }
    view.layoutIfNeeded()
}

reference link AutoLayout: UIImageView and Aspect Fit content mode

junaidsidhu
  • 3,539
  • 1
  • 27
  • 49