Skip to content
/ SVGKit Public
forked from reklis/SVGKit

A Cocoa framework for rendering SVG files as Core Animation layers

License

Notifications You must be signed in to change notification settings

stolzda/SVGKit

 
 

Repository files navigation

SVGKit

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!

Usage - Basic (iPhone/iPad)

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"]]];

Usage - Intermediate (iPhone/iPad)

OPTION 1: Load an SVG file, and convert the document to CALayer's which Apple can render

  1. SVGKImage *im = [SVGKImage imageNamed:@"my_svg_file"]; //this loads the file, parses the SVG, and outputs an SVGImage object
  2. [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

  1. SVGKImage *im = [SVGKImage imageNamed:@"my_svg_file"]; //this loads the file, parses the SVG, and outputs an SVGImage object
  2. 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

Advanced Features (this branch/fork only!) (iPhone/iPad)

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

FEATURE 2: load SVG from web, or from disk

FEATURE 3: search an SVG file for particular tags / nodes / elements:

  • 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:@"*"];

FEATURE 4: resize your SVG file to any size:

FEATURE 5: automatic scaling of your SVG to fit in memory

  • 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

FEATURE 6: Access to the DOM Document Object Model

  • PARTIALLY IMPLEMENTED: SVGKImage.DOMDocument is a true DOMDocument, but many of the methods aren't fully implemented

FEATURE 7: Retrieve any part of your SVG document positioned correctly in space

  • [((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

FEATURE 8: detailed information on whether and WHY parsing failed:

  • (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

Usage - OS X

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

Installation

Dependencies:

git submodule init && git submodule update

iOS (iPhone/iPad):

  1. Open the project "XcodeProjects/SVGKit/SVGKit"

  2. Select the target "SVGKit Library" from the dropdown build selector at top left

  3. Build

  4. Look in your output directory for a folder named "debug-universal" - this contains a library + headers for: iPhone, iPad, iOS Simulator

  5. Drag/drop the library file, and the headers folder (should be called "usr") into your iPhone/iPad project.

  6. Edit your build settings and set "C/C++ Compiler Version" = "LLVM Compiler 2.0"

  7. Edit your build settings and add "Other Linker Flags" = "-ObjC"

  8. (Optional but recommended): Edit your build settings and add "Header Search Paths" = "/usr/include/libxml2"

  9. (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.

About

A Cocoa framework for rendering SVG files as Core Animation layers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Objective-C 99.6%
  • Other 0.4%