Search code examples
asset-catalogxcode9

Xcode 9 asset catalog Preserves Vector Data not working?


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 seen at two zooms in Preview:

enter image description here

enter image description here

Nice and sharp with lots of zoom, so clearly this is a vector image. But here's what two image views look like in my app:

enter image description here

So where's my vector data? Is this much-desired feature still missing in action? Does it still work only for the automatically generated 2x and 3x images? And if so, what does the Preserve Vector Data checkbox give us that we didn't have already?


Solution

  • It works, but only if you perform the resizing yourself:

    enter image description here

    That was achieved in code, like this:

        let im = UIImage(named:"Image")!
        let r = UIGraphicsImageRenderer(size:self.iv2.bounds.size)
        let im2 = r.image {
            _ in
            im.draw(in: self.iv2.bounds)
        }
        self.iv2.image = im2
        self.iv2.contentMode = .center
    

    So UIImageView will rasterize as it scales (e.g. for Aspect Fit), but drawing in code will preserve the vector data.

    EDIT New in Xcode 9 beta 5, this now works as expected! In this screen shot, the second image view just does a scale-to-fill, no more. We resize sharply!

    enter image description here

    EDIT In playing around with Xcode 11 I have finally found a formula that always works. This means that on launch, with no extra code, in an image view or elsewhere, a vector-based image appears sharp at any size.

    In the asset catalog, you must set the Scales pop-up menu to Individual Scales and put the vector-based image into the 1x slot. Check Preserve Vector Data. Done.