我在我的 xcassets 中使用一些 pdf 图像,并选中了“保留矢量数据”。相同的图像在不同的 iOS 版本中呈现不同的效果(我使用的是 Xcode 11.2)。左侧,图像在 iOS 13.2 中按预期渲染,右侧图像在 iOS 11.4 中未按预期渲染(iOS 12.2 中相同)。通过删除对“保留矢量数据”的检查,问题就不会出现。
创建 UIImageViews 的代码是这样的:
self.view.backgroundColor = .white
let imageView = UIImageView(frame: CGRect(x: 10, y: 20, width: self.view.bounds.width - 20, height: self.view.bounds.width - 20))
imageView.image = UIImage(named: "calendar")
imageView.backgroundColor = UIColor.red.withAlphaComponent(0.4)
imageView.contentMode = .center
self.view.addSubview(imageView)
Pdf 图像大小为 27x23 像素,通过停止模拟器并打印出图像大小和比例仍然可以打印正确的值:
(lldb) po ((UIImageView *) 0x7fa1b0c053e0).image.scale
2
(lldb) po ((UIImageView *) 0x7fa1b0c053e0).image.size
(width = 27, height = 23)
我错过了什么吗?
最佳答案
我知道这听起来很奇怪,但是 UIImageView 属性设置中的一点切换解决了这个问题:
let imageView = UIImageView(frame: CGRect(x: 10, y: 20, width: self.view.bounds.width - 20, height: self.view.bounds.width - 20))
imageView.contentMode = .center
imageView.image = UIImage(named: "calendar")
imageView.backgroundColor = UIColor.red.withAlphaComponent(0.4)
self.view.addSubview(imageView)
首先设置contentMode
,然后设置image
。
关于ios - UIImageView 中的矢量图像在 iOS 12 和 13 中呈现不同的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58747949/