I like to convert an .jpg or .png file to an .svg format that can be displayed in UIImageView. Is there a way to do this in Objective-C?
-
you mean vectorizing the image or embeding it in an svg ? – n00dl3 Sep 27 '16 at 10:53
-
Why would you want to convert it to SVG? The file becomes bigger. The quality doesn't increase. And UIImageView is built for PNG and JPEG. – Codo Sep 27 '16 at 11:17
-
Yes i want convert in vectorize image @n00dl3 – Mehul Valand Sep 28 '16 at 12:26
2 Answers
You shouldn't use SVG images in Xcode:
it is recommended that you use PNG or JPEG files for most images in your app. Image objects are optimized for reading and displaying both formats, and those formats offer better performance than most other image formats. Because the PNG format is lossless, it is especially recommended for the images you use in your app’s interface.
Also, there's an SVGKit that according to many devs is buggy so use it at your own risk.
SVG is an XML-based vector and in Xcode you can also use vectors but using a PDF format and follow this tutorial.
In a nutshell:
- Generate PDFs With the @1x Asset (During compile time it will generate @2 and @3)
- Set the Scale Factors to Single Vector:
- Drag and Drop Your PDF Into the All, Universal Section
Refer to Your Image by Its Name, Like for any PNG File
[UIImage imageNamed:@”Home”]
Also, stackoverflow (perhaps) related answer
-
4
-
-
2
-
I believe this answer is quite dated now. As of [Xcode 12](https://developer.apple.com/documentation/xcode-release-notes/xcode-12-release-notes), there is SVG support: > Asset Catalogs New Features Added support for Scalable Vector Graphic (SVG) image assets. These preserve their vector representation with deployment targets of macOS 10.15 or later, iOS 13 or later, and iPadOS 13 or later. (18389814) – RawKnee Aug 26 '22 at 21:56
To convert PNG to SVG (Which is a very sensible and valid thing to do) you need an app or library called a "tracer". It will trace the outlines of shapes, gradients, etc and convert them into vector representations.
For simple cases, this is easy for a computer to do; for complex cases (e.g. gradients), this is a very hard AI / Computer-Vision problem and you'd need to spend $$$ on high-end coding solutions and/or find PhD-level research that solves the problem!
A free tracer that does a very good job is built-in to Inkscape (open-source) - google "inkscape trace" for tutorials on how to use it, and to generate a .svg file that you can use. This is a manual process.
To use this in-app, you need to find a tracing library for iOS. The libraries that Inkscape uses are all open-source, so you could try converting them to iOS - they're written in C, so it could be quite easy.

- 32,900
- 16
- 126
- 153