Xcode 9 asset catalog Preserves Vector Data not working?

前端 未结 4 1244
一整个雨季
一整个雨季 2020-12-24 07:21

I thought the new Preserves Vector Data checkmark in the Xcode 9 asset catalog would finally give us resizing of vector PDF images, but apparently not. Here\'s my test image

4条回答
  •  伪装坚强ぢ
    2020-12-24 07:36

    In my case (Xcode 9.4.1), with imageView created in Interface Builder - I noticed that when I initially arrive on the screen, the image is blurry. If I then change device orientation, the image becomes crisp. I tried calling different methods manually in viewDidLoad() and here is what I found so far:

    This worked:

    let image = imageView.image
    imageView.image = nil
    imageView.image = image
    

    None of these worked:

    imageView.layoutSubviews()
    imageView.layoutMarginsDidChange()
    imageView.setNeedsLayout()
    imageView.setNeedsDisplay()
    imageView.reloadInputViews()
    imageView.updateConstraints()
    imageView.contentMode = .center ; imageView.contentMode = .scaleToFill
    

    You should of course extend or subclass UIImageView if you'll be calling it often, like this for example

    class UIImageViewWithPreserveVectorDataFix: UIImageView {
        override func awakeFromNib() {
            super.awakeFromNib()
            let image = self.image
            self.image = nil
            self.image = image
        }
    }
    

    (and then of course set UIImageViewWithPreserveVectorDataFix as the class in Interface Builder)

提交回复
热议问题