图片显示不正确,我要显示像Facebook图像帖子

问题描述:

我必须尝试所有类型的内容模式,第三方和大量的谷歌搜索。 但是,对我来说不成功。 我想显示像Facebook图像文章。 我们在facebook,facebook上显示正确的图像并将图像非常正确地填充到图像视图中。图片显示不正确,我要显示像Facebook图像帖子

+2

你能添加你的要求截图?到目前为止你做了什么? –

+0

你检查了imageViewContentMode吗?尝试将内容模式设置为AspectFit/Fill。如果可能的话就这个问题提供更多的见解 –

按照步骤

  1. setImage到UIImageView的(与UIViewContentModeScaleAspectFit
  2. GET IMAGESIZE (CGSize imageSize = imageView.image.size)
  3. UIImageView的大小调整。 [imageView sizeThatFits:imageSize]
  4. 移动你想要的位置。

    CGSize imageSize = imageView.image.size; 
        [imageView sizeThatFits:imageSize]; 
        CGPoint imageViewCenter = imageView.center; 
        imageViewCenter.x = CGRectGetMidX(self.contentView.frame); 
        [imageView setCenter:imageViewCenter]; 
    
+1

谢谢@ junaidsidhu。我想填充像Facebook的图像。任何类型的图像(如高度>宽度,宽度(高度* 2)>高度)通过Facebook填充非常正确。 –

集的UIImageView大小==图像(比例):

@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() 
} 

参考链接AutoLayout: UIImageView and Aspect Fit content mode