SVGKit is a Cocoa framework for rendering SVG files as Core Animation layers. All shapes are represented by instances of the CAShapeLayer class, and are, by design, animatable. SVGKit is compatible with the latest iOS SDK's.
This specific branch contains a MASSIVE re-factor of the original SVGKit, aiming to make it easier to use in your apps. Please read the "usage" instructions carefully - they have changed from previous SVGKit versions!
Instantiate an SVGKImageView using the filename of an SVG file, and add it to your view with [UIView addSubview:]
[self.view addSubview: [[SVGKFastImageView alloc] initWithSVGKImage: [SVGKImage imageNamed:@"mySVGfile.svg"]]];
OPTION 1: Load an SVG file, and convert the document to CALayer's which Apple can render
- SVGKImage *im = [SVGKImage imageNamed:@"my_svg_file"]; //this loads the file, parses the SVG, and outputs an SVGImage object
- [self.view.layer addSublayer:im.CALayerTree]; // SVGKImage can export itself as Apple's CALayer's
...and if you want to display the same SVG somewhere else simultaneously, you don't have to re-parse it, you can just call: 3. [self.view.layer addSublayer:[im newCALayerTree]; // Creates a clone of the CALayers, you can edit without affecting originals
OPTION 2: Load an SVG file, and read the SVG data directly by looking at the tree of SVGElement subclasses
- SVGKImage *im = [SVGKImage imageNamed:@"my_svg_file"]; //this loads the file, parses the SVG, and outputs an SVGImage object
- SVGSVGElement* rootOfTree = im.DOMTree; // NB: this is a partial implementation of the official "SVG DOM" standard. See the header file for this class and its superclass to see what you can do with it
FEATURE 1: use an SVG just like it's a normal PNG file: use SVGKFastImageView like it's UIImageView:
- SVGKImage = equivalent of UIImage (same methods and properties - some not implemented, but all core items implemented)
- SVGKFastImageView = equivalent of UIImageView (same methods, with extra properties to support the features of SVG that plain bitmaps lack - e.g. resolution independent rendering)
...NB: by default, if you change the "frame" property of an SVGKFastImageView, it automatically re-renders the SVG at the new resolution. ...NB: bugs in Apple's UIScrollView mean you MUST disable the above feature before allowing user's pinch-zoom: a property on SVGKFastImageView lets you turn this on/off
-
[SVGKParser parse: (SVGKSource*)]; // anything that's an "SVGKSource" can be parsed
-
[SVGKSource sourceWithFile:@"monkey.svg"]; // create a source from disk...
-
[SVGKSource sourceWithURL:@"http://upload.wikimedia.org/wikipedia/commons/f/fd/Ghostscript_Tiger.svg"]; // create a source from disk...
- Find all svg:g and tags: NodeList* gElements = [svgImage.DOMDocument getElementsByTagName:@"g"];
- Find ALL tags (from root of SVG down): NodeList* allElements = [svgImage.DOMDocument getElementsByTagName:@"*"];
- COMPLICATED: c.f. this thread: https://github.com/adamgit/SVGKit/issues/7
- AUTOMATIC: SVG files are scaled to fit the co-ordinate system as required/intended by spec
- ...many files that ran out of memory in previous versions of SVGKit now render OK
- PARTIALLY IMPLEMENTED: SVGKImage.DOMDocument is a true DOMDocument, but many of the methods aren't fully implemented
- [((SVGKImage*) image).newCopyPositionedAbsoluteLayerWithIdentifier:@"id of the SVG tag / node /element"];
- NB: this MUST return a copy, because it's moving your layer out of the tree and into a fresh CALayer of its own
-
(SVGKParser*).currentParseRun.warnings; // array of NSError objects, each one a "WARNING" from the parser
-
(SVGKParser*).currentParseRun.errorsFatal; // array of NSError objects, each one a "FATAL ERROR" from the parser - if your SVG didn't render at all, this is why!
-
(SVGKParser*).currentParseRun.errorsRecoverable; // array of NSError objects, each one a "RECOVERABLE ERROR" from the parser - if your SVG didn't render correctly, this is why! (although you probably still got to see something)
-
(SVGKImage*).parseErrorsAndWarnings; // this is a convenience pointer to (SVGKParser*).currentParseRun used above
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!
Feel free to report any issues or suggest improvements in the issue tracker
Dependencies:
git submodule init && git submodule update
iOS (iPhone/iPad):
-
Open the project "XcodeProjects/SVGKit/SVGKit"
-
Select the target "SVGKit Library" from the dropdown build selector at top left
-
Build
-
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"
-
(Optional but recommended): Edit your build settings and add "Header Search Paths" = "/usr/include/libxml2"
-
(Optional but recommended): Add the framework "libxml2.dylib"
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.