xcodevector-graphics

How do vector images work in Xcode (i.e. pdf files)?


How does vector support work in Xcode 6?

When I try resizing an image, it looks jagged, what gives?


Solution

  • How to use vectors in Xcode (7 and 6.3+):

    1. Save an image as a .pdf file at the proper @1x size (e.g. 24x24 for a toolbar button).
    2. In your Images.xcassets file, create a new Image Set.
    3. In the Attributes Inspector, set the Scale Factors to Single Vector.
    4. Drag and drop your pdf file into the All, Universal section.
    5. You can now refer to your image by its name, just as you would any .png file.

      UIImage(named: "myImage")
      

    How to use vectors in older versions of Xcode (6.0 - 6.2):


    How vectors work in Xcode

    Vector support is confusing in Xcode, because when most people think of vectors, they think of images that can scale up and down and still look good. However, Xcode 6 and 7 don't have full vector support for iOS, so things work a little differently.

    The vector system is really simple. It takes your .pdf image, and creates @1x.png, @2x.png, and @3x.png assets at build time. (You can use a tool to examine the contents of Assets.car to verify this.)

    For example, assume you are given foo.pdf which is a 44x44 vector asset. At build time it will generate the following files:

    This works the same for any sized image. For example, if you have bar.pdf which is 100x100, you will get:


    Implications:

    Why it (probably) works this way: