2017-04-18 71 views
0

所以我试图从URL中显示图像到图像中(连接槽IBOutlet)。但是,图像确实会显示,但不会遵循故事板中设置的样式规则。如:看点填充。这会导致图像重叠模拟器中的所有内容。来自URL的图片不遵循故事板的样式规则

代码:

let url = URL(string: "http://www.wsvh.nl/wp-content/uploads/bekergoud.jpg") 

    DispatchQueue.global().async { 
     let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check/try-catch 
     DispatchQueue.main.async { 
      self.nieuwsFeaturedImage.image = UIImage(data: data!) 
     } 
    } 

结果: enter image description here

你可以看到图像重叠的标题和描述。

我该如何解决这个问题?

+0

尝试'[UIImage的imageWithData:数据规模:UIScreen mainScreen] .scale]' – Brandon

回答

2

如果您设置了方向填充,您还需要设置nieuwsFeaturedImage.clipsToBounds = true或在Interface Builder中检查Clips to Bounds框。

没有它,您的图像会根据比例进行缩放,因此它会扩展到您指定的内容区域之外。您需要截断所有额外的内容 - 剪辑到边界就是为了这个。

enter image description here

+0

在极少数情况下,该解决方案将无法工作。在这种情况下,您需要在代码中使用'[UIImage imageWithData:数据比例:[UIScreen mainScreen] .scale]'将正确的比例应用于图像。IE:@即使启用了ClipToBounds 。 – Brandon