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.
Asked
Active
Viewed 93 times
-1
-
2Can you add a screenshot of your requirement? and what exactly you have done till now? – Anurag Sharma Jul 10 '17 at 06:49
-
Did you check imageViewContentMode? try setting content mode to AspectFit/Fill. If possible give more insight on this problem. – Sivajee Battina Jul 10 '17 at 06:56
2 Answers
0
Follow steps
- setImage to UIImageView (with
UIViewContentModeScaleAspectFit
) - get imageSize
(CGSize imageSize = imageView.image.size)
- UIImageView resize.
[imageView sizeThatFits:imageSize]
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
-
1Thanks @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