我在xcasset中使用了一些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
。