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

标签 ios xcode uiimageview xcasset contentmode

我在我的 xcassets 中使用一些 pdf 图像,并选中了“保留矢量数据”。相同的图像在不同的 iOS 版本中呈现不同的效果(我使用的是 Xcode 11.2)。左侧,图像在 iOS 13.2 中按预期渲染,右侧图像在 iOS 11.4 中未按预期渲染(iOS 12.2 中相同)。通过删除对“保留矢量数据”的检查,问题就不会出现。

enter image description here

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

相关文章:

objective-c - 自定义 Storyboard对象

ios - Cocos2d 中的无限水平滚动

ios - 在 Swift 中单击 UILocalNotification 时如何启动特定的 ViewController

ios - 在 Xcode 8 上出现 "SpringBoard was unable to service the request"错误

xcode - 如何知道哪个物体发生碰撞?

ios - 剃掉 UIImageView 的一侧?

ios - 如何使用 UIKit 更快地渲染具有效果的图像

ios - 设置 self 时无法正确更新 float 的 UILabel 文本的函数(仅适用于 super)

ios - UIImageView mask 层不来自角落直到滚动 PageViewController 第一次

ios - UILabel - 多行