iosxcodeuiimageviewxcassetcontentmode

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


I'm using some pdf images in my xcassets with "preserve vector data" checked. The same image is rendered differently across different iOS versions (I'm using Xcode 11.2). On the left, the image is rendered as expected in iOS 13.2, on the right the image is not rendered as expected in iOS 11.4 (same in iOS 12.2). By removing the check on "preserve vector data" the issue doesn't show up.

enter image description here

The code to create the UIImageViews is this:

    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 image size is 27x23 pixels, and by stopping the simulator and printing out the image size and scale is still print the correct values:

(lldb) po ((UIImageView *) 0x7fa1b0c053e0).image.scale
2

(lldb) po ((UIImageView *) 0x7fa1b0c053e0).image.size
(width = 27, height = 23)

Am I missing something?


Solution

  • I know that could sound weird, but a little switch in UIImageView properties setting solves the issue:

    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)
    

    Set first contentMode, then the image.