UIImageView 中的矢量图像在 iOS 12 和 13 中呈现不同

Vector images in UIImageView are rendered differently in iOS 12 and 13

我在我的 xcassets 中使用了一些 pdf 图像,并勾选了 "preserve vector data"。同一图像在不同的 iOS 版本中呈现不同(我使用的是 Xcode 11.2)。在左侧,图像在 iOS 13.2 中按预期渲染,在右侧,图像在 iOS 11.4 中未按预期渲染(在 iOS 12.2 中相同)。通过删除 "preserve vector data" 上的检查,问题不会出现。

创建 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