SVGKit is a Cocoa framework for rendering SVG files natively: it's fast and powerful.
- v1.x = current "in development" branch with latest changes, fixes, features
- NB: this is now automatically selected in GitHub as the "default" branch when you visit SVGKit's project page
- v1.1.0 = most recent "stable" release; works with iOS7, has fixes + adds basic (slow) Text support
- v1.0 = Major refactor/re-write at start of 2013, approximately 90% coverage of the SVG spec. Stable and quite fast - https://github.com/SVGKit/SVGKit/tree/v1.0
- MaddTheSane : this fork adds a complete working build of SVGKit for Mac OS X, and a (very) large number of tweaks (over 10k lines of code). We need help integrating + checking these changes against master / 1.x - please get in touch (create an issue, or email adam.m.s.martin at gmail) if you can help.
For 2013, we have a major refactor/rewrite of the core SVGKit, fixing many bugs, adding many features, and making SVGKit much closer to meeting the SVG official Specification - http://www.w3.org/TR/SVG/
Because so much has changed, Adam wrote a series of blog posts to explain how to use the latest versions:
-
GETTING STARTED, plus NEW FEATURES: http://t-machine.org/index.php/2012/12/31/svgkit-2013-usage/
-
QUICK RECIPES for common uses: http://t-machine.org/index.php/2013/01/02/svgkit-2013-recipes/
- additiona: How to scale an SVG image on screen: http://t-machine.org/index.php/2013/04/14/svgkit-scaling-svg-images/
-
CONTRIBUTING to the project, CORE ARCHITECTURE: http://t-machine.org/index.php/2012/12/31/svgkit-2013-development/
-
(November 2013): New (experimental) feature - writing SVG's out to disk, preserving any changes you made programmatically: http://t-machine.org/index.php/2013/11/17/svgkit-programmatic-editing-of-svg-files-on-ios/
Open the project "Demo-iOS", select your preferred simulator (or device, if it's plugged in), build and run. Try the sample SVG's packaged with SVGKit.
To use SVGKit, either CREATE AND DISPLAY AN IMAGE ON SCREEN:
[self.view addSubview: [[SVGKFastImageView alloc] initWithSVGKImage: [SVGKImage imageNamed:@"mySVGfile.svg"]]];
or LOAD AN IMAGE AND CONVERT TO A UIIMAGE OBJECT:
UIImage* newImage = [SVGKImage imageNamed:@"mySVGfile"].UIImage;
WARNING: Due to some bugs in Apple's renderer, SVGKFastImageView doesn't render gradient-fills for anything except rectangles, and cannot use CoreAnimation. For higher accuracy (but slightly worse performance), use SVGKLayeredImageView instead.
Key methods / properties you might find useful:
- SVGKImage - all the magic happens here
- SVGKImage.DOMTree - a complete DOM tree of the "parsed" SVG/XML document, with ALL TAGS (even if unsupported by current SVGKit - they will be parsed anyway!)
- SVGKImage.CALayerTree - a complete, renderable, tree of Apple CALayer objects (only contains layers for features currently supported - if you find something missing, please submit a fix!)
If you get this error:
"+[NSCharacterSet SVGWhitespaceCharacterSet]: unrecognized selector sent to class "
... you're probably building the Static Library, but forgot to do Step 7 below:
"Edit your build settings and add "Other Linker Flags" = "-ObjC""
There are many new features in this version (2013) of SVGKit. Read the blog posts above for more detail, but briefly:
- use an SVG just like it's a normal PNG file: use SVGKFastImageView like it's UIImageView:
- load an SVG from web, or from disk
- search an SVG file for particular tags / nodes / elements:
- automatic scaling of your SVG to fit their on-screen size (often reduces the memory required)
- Access to the DOM Document Object Model
- Retrieve any part of your SVG document positioned correctly in space
- detailed information on whether and WHY parsing failed:
UNSUPPORTED: the code exists, but none of the maintainers have used it recently, so we're not even sure if the OS X build still works!
OS X: ...this needs updating; some minor code changes are needed to "fix" this project. The OS X framework currently DOES NOT BUILD because iOS classes are being referenced in a couple of places.
Feel free to report any issues or suggest improvements in the issue tracker
There is a sample project you can examine which embeds the entire SVGKit project into an application project. Many people like working this way (although Xcode isn't very good at it. Open the "Demo-iOS" project and poke around.
Alternatively, most people like to compile an embeddable library:
-
Open the project "SVGKit-iOS"
-
Select the target "SVGKit Library" from the dropdown build selector at top left
-
Build 4a. (open the Products section on left hand bar in Xcode, right click the product, and select "Show in Finder". THEN GO UP ONE DIRECTORY! You MUST NOT USE the Product that Xcode shows you - it IS WRONG, it is a BUG IN APPLE'S XCODE)
-
Look in your output directory for a folder named "debug-universal" - this contains a library + headers for: iPhone, iPad, iOS Simulator
-
Drag/drop the library file, and the headers folder (should be called "usr") into your iPhone/iPad project.
-
Edit your build settings and set "C/C++ Compiler Version" = "LLVM Compiler 2.0"
-
Edit your build settings and add "Other Linker Flags" = "-ObjC"
-
Add all the REQUIRED APPLE FRAMEWORKS (see below)
-
(Optional but recommended): Edit your build settings and add "Header Search Paths" = "/usr/include/libxml2"
-
(Optional but recommended): Add the framework "libxml2.dylib"
You must add the following frameworks for SVGKit to compile - unless you are embedding the whole project file (in which case Apple does it automatically)
- CoreText
- CoreImage
- libxml2.dylib
- QuartzCore
- CoreGraphics
- UIKit
(only the first three are unusual - the rest you probably have by default in your iOS project template)