ios - UIImageView 中的矢量图像在 iOS 12 和 13 中呈现不同
问题描述
我在我的 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
。
推荐阅读
- javascript - 如何在 JS 中复制对象表单控制台
- c++ - 为什么即使派生类不添加成员,继承具有非虚析构函数的类也不是一件好事?
- php - 我的用户登录工作正常,我的管理员登录根本不起作用
- python - 如何在 python 中让 2016 年 2 月 18 日小于 2016 年 3 月 1 日?
- android - 您试图通过调用 firebase.auth() 来使用未安装在您的 Android 项目上的 firebase 模块
- javascript - NodeJS MongoDB:我如何获取数据然后同步返回
- mysql - MYSQL 触发器,是否可以循环一次然后禁用字段更新?
- javascript - 使用 Styled Components 无法使整个背景具有某种颜色
- css - 如何将 Woocommerce 产品库的导航控件放在右侧>
- r - Using purrr and functions to perform linear regressions on a number of variables with random errors