SVGKit is a Cocoa framework for rendering SVG files natively: it's fast and powerful.
Versions:
- v1.0 - 2013, approximately 90% coverage of the SVG spec. Stable and quite fast - https://github.com/SVGKit/SVGKit/tree/v1.0
- v1.1.0 - Current branch in development; all pull-requests should be issued against this branch - https://github.com/SVGKit/SVGKit/tree/1.1.0
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/
- CONTRIBUTING to the project, CORE ARCHITECTURE: http://t-machine.org/index.php/2012/12/31/svgkit-2013-development/
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!)
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
- QuartzCore
- CoreGraphics
- UIKit
(only the first two are unusual - the rest you probably have by default in your iOS project template)