Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 6 allows VECTOR image assets... how to use them?

I was fiddling with Xcode 6 vs images assets when I noticed something very interesting: we now can specify vector images in them (go see in the Utilities pane after selecting Images.xcassets).

I tried a small app (containing a big UIImageView) with a .SVG image (didn't work), then a .EPS (didn't work either) and I finally tried a .PDF It worked! Well, although I saw the image, It appeared pixellated and not vectorized.

So it seems Apple is preparing the way for vector icons/images. No more zillions versions of app icons, no more "@2x" images. But can anyone unlock that feature?

like image 937
Jean Le Moignan Avatar asked Jun 10 '14 19:06

Jean Le Moignan


People also ask

Can xcode use SVG files?

Updates from Apple's 2020 WWDC Conference revealed that Xcode 12 will now support the SVG (Scalable Vector Graphics) file format. SVGs are a format of vector graphics known to be smaller files than other vector graphics formats, and as their name implies SVGs scale well.

Does iOS support SVG images?

SVG is supported by all major modern web browsers and for IOS it's mostly used for symbols and icons and helps platforms to scale the asset to the current screen size.

What does preserve vector data do in Xcode?

If you enable “Preserve Vector Data” this feature comes to your apps with no additional work. By enabling this feature, iOS 11 can also automatically scale images regardless of whether you're increasing a UIImageView 's bounds, or using Size Classes to change an UIImageView size.


2 Answers

Here're some of my thoughts after some experiments on vector assets:

1. Compile time support

After several trials, I believe that it's just a compile time support. Xcode generate all the 1x, 2x and 3x images at compile time. Which means it works with older iOS versions. At the same time, it means that in the final build, it's still in PNG format, and you cannot get lager lossless image from the vector file.

2. Why PDF instead of SVG or other formats

For SVG and other formats, the vector image has no actual size info, while PDF has size info. I think what Xcode 6 does is using the size info in PDF as actual display size, then generate 2x 3x files from the vector image.

Size info in PDF

3. File size of PDF does not matter

At the begining, our concern is that PDF will be much bigger than PNGs. We tried http://smallpdf.com/ to compress it and it work pretty well. But if the original PDF file is not included in the build as I said before, then the file size of PDF does not matter.

Will continue editing this post if I find any other things.

EDIT 14-09-25

@mredig mentioned that for iOS, it generates bitmaps at compile time, but for OSX it includes the vector image in a scalable form.

via: http://martiancraft.com/blog/2014/09/vector-images-xcode6/

like image 91
Allen Hsu Avatar answered Oct 13 '22 12:10

Allen Hsu


Here's how to experiment with vector images in the asset catalog in Xcode 6:

  1. Make a new image set.

  2. Select a blank image slot in your image set and switch the pop-up in the attributes inspector to Vectors. You now have a single universal image slot.

  3. Drag a vector PDF into that slot.

Now, wherever that image is used, it is sized to its context (e.g. a fixed-size image view) without rasterization, as shown in this screen shot:

enter image description here

EDIT Despite this answer, the larger PDF drawing was rasterizing. But now, see https://stackoverflow.com/a/45623000/341994 : in Xcode 9, the vector PDF scales properly, without rasterizing.

EDIT In Xcode 11, this formula works: 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.

like image 31
matt Avatar answered Oct 13 '22 10:10

matt