SVGKit is a Cocoa framework for rendering SVG files natively: it's fast and powerful. Some additional info and links are on the wiki
- master - Current branch in development. Has code from SVGKit's 1.1.0 branch - https://github.com/SVGKit/SVGKit/tree/1.1.0 as well as a lot of enhancements including OS X support and "modern" Objective-C syntax.
- v1.x = current "in development" branch with latest changes, fixes, features
- v1.1.0 - Branch that was used to merge changes from SVGKit's 1.1.0 branch from SVGKit's master. Now defunct: It has been merged with master. Occasionally the branch will be used as a staging branch for merges to go back to master.
- 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
- arc - Branch that has the iOS and OS X project use Automatic Reference Counting. Now defunct: it has been merged back to master.
- namespaces - Branch that has the SVGKit headers better designed to work with OS X. Now defunct: it has been merged back to master.
- toSVGKit - Branch that contains staged changes to put back into SVGKit's 1.1.0 branch.
- patterns - Branch for working on pattern support. based off of SVGKit's 1.1.0 branch as opposed to my master. Parts have been merged back to master.
- noAsserts - Most of the NSAsserts are changed into DDLogErrors.
- colorFlags - The SVGColor struct has a flag for the status of the creation of the color struct.
- cleanup - Merging my master to the original version's 1.x branch.
NB: April 2015: new Demo app, many more features, may have teething troubles with this one.
- Open up "Demo-iOS.xcodeproj", and run it (on simulator or device). Try different SVG's. Zoom, pan, and (with the Monkey only:) hit the "Animate" button. Tap the images to see bounding-boxes / hit dectection (might need you to hit the Debug button first)
- If you have ANY problems building the library and embedding it in your app, compare your build settings to the Demo-iOS build settings - if something's different, it's probably the problem.
We use this build script to automatically build ALL versions of the library at once, and ship them as a single file: http://stackoverflow.com/questions/3520977/build-fat-static-library-device-simulator-using-xcode-and-sdk-4/3647187#3647187
It's all setup already, all you need to do is:
- Open "SVGKit-iOS.xcodeproj", and Build it (cmd-B)
- in left navbar, scroll to bottom, and open the "Products" section
- right click the library ("libSVGKitBLAHBLAH.a") and select "show in finder"
- GO UP ONE FOLDER
- select the "Debug-universal" (or Release-universal if you were building in Release mode) folder
- Drag/drop the .a file and the "usr" folder into your project (select the "Copy files" checkbox)
- In Build Settings, select "Other Linker Flags" and add "-ObjC"
- Edit your build settings and set "C/C++ Compiler Version" = "LLVM Compiler"
- Edit your build settings and add "Other Linker Flags" = "-ObjC"
- Add ALL the frameworks and 3rd party libraries listed below (go to "Build Phases", and "Link Binary with Libraries"):
- CoreText
- CoreImage
- libxml2.dylib
- QuartzCore
- CoreGraphics
- UIKit
Everything else is automatic.
However, early versions of Xcode5 have a major bug, affecting all libraries:
- Xcode 5: Apple will create a hardlink to your local hard-disk, breaking your project for all your co-workers. This is a new bug added for Xcode5 :(
- Xcode5: in Build Settings, scroll down to the "Library Search Paths"
- Xcode5: double click it. If it has any absolute paths, chop off the start, and replace the start with ""$(SRCROOT)/"" (note the quotes inside are necessary - Apple's Xcode has another bug where it will fail to build if you have spaces in any of your folder names!)
Instead of fixing the bug, in latest Xcode 5.0.2, Apple has simply made it allow two copies of every Search Path. Hmm.
Here are some posts on using SVGKit, with advice on which methods to use and why:
-
Getting Started, plus new features: http://t-machine.org/index.php/2012/12/31/svgkit-2013-usage/
-
Quick Recipies 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/
If you get this error:
"+[NSCharacterSet SVGWhitespaceCharacterSet]: unrecognized selector sent to class "
... you're probably building the Static Library, but forgot to do the step below:
"Edit your build settings and add "Other Linker Flags" = "-ObjC""
Open the project "Demo-OSX", select either Demo-OSX, ImageRepTest, or ImageRepTest-NS, build and run.
To use SVGKit, either create and display the image on screen:
[view addSubview: [[SVGKLayeredImageView alloc] initWithSVGKImage: [SVGKImage imageNamed:@"mySVGfile.svg"]]];
or load an image and convert it to an NSImage or NSBitmapImageRep object:
NSImage *newImage = [SVGKImage imageNamed:@"mySVGfile"].NSImage;
NSBitmapImageRep *newImageRep = [SVGKImage imageNamed:@"mySVGfile"].bitmapImageRep;
or load the SVG using NSImage:
NSImage *svgImage = [NSImage imageNamed:@"mySVGfile.svg"];
There are three ways to make SVGKit run on OS X:
- Use SVGKImageRep. Load the SVGKit framework via direct linking or NSBundle. You can then use NSImage to load SVGs like any other NSImage-capable file. Note that it has the same limitations as SVGKFastImageView.
- SVGKLayeredImageView is the suggested way to display SVGs on OS X. Note that if you add the view subclass via Interface Builder, the framework will complain when the view is inited.
- SVGKFastImageView is another subclass of NSView that you can use. Note that there currently is a bug that text will be upside-down unless the CALayer is also being used by a SVGKLayeredImageView. Also, like the iOS version, scaling and gradients don't work.
Note that you can only build SVGKit as a framework (which will include the Lumberjack code as a subframework) For loading the framework at run time, add the following line to the rpath
linker options:
@loader_path/../Frameworks
There is a sample project you can examine which copies the SVGKit framework into the Frameworks subdirectory of the app and sets the runpath to @loader_path/../Frameworks to load the framework. To include an SVGKit header is the same as any other OS X framework, by including <SVGKit/HeaderName.h>.
To use SVGKImageRep, either just link to SVGKit, or load the SVGKit framework with NSBundle. Both methods will add the SVGKImageRep class to the NSImageRep classes list.