How to import/parse SVG into UIBezierpaths, NSBezierpaths, CGPaths? [closed]

和自甴很熟 提交于 2019-12-02 19:25:49

Alright folks, if you're willing to pay for it. This tool will solve all your issues. You can import svg or psd and will produce the code for you. I've used it and it is amazing!

http://www.paintcodeapp.com/

There's an open-source SVG library in ObjectiveC, specifically for iOS (and OS X, although the OS X build is a bit behind):

https://github.com/SVGKit/SVGKit/

(I've been using this on iOS projects since the start of this year, it works well)

2018 update: OSX support is now in, and SVGKit renders most SVG's fast + accurately. On the downside, advanced text features aren't all supported (e.g. yes to gradient-fill, but no to complex text-layout).

As of early 2012, it's not a 100% implementation of the SVG spec, but there's a lot of people working on getting it there.

If you're willing to do some translation from JS to ObjC, then you might find this project helpful:

https://github.com/thelonious/svg-2d

Particularly, the path.js file which includes a parseData method, taking care of relative and absolute path commands.

https://github.com/thelonious/svg-2d/blob/master/shapes/path/Path.js

I should mention that I wrote this code years ago, but I have translated it to Java and C# and have used in professional products (can't share that code, unfortunately) and it seemed to work well in those environments as well.

HTH Kevin

Sketch 3 on Mac App Store is good at editing (can be better than PaintCode) and then exporting as SVG to PaintCode.

To get from PaintCode back to Sketch save as PDF and imports ok

https://itunes.apple.com/gb/app/sketch-3/id852320343?mt=12

Make sure canvas size in both is set to the same and in your views bounds else youll need to scale the context.

SVGgh contains a class, SVGPathGenerator, which will convert an SVG path to a CGPathRef:

+(CGPathRef) newCGPathFromSVGPath:(NSString*)anSVGPath whileApplyingTransform:(CGAffineTransform)aTransform;

You can use it to do exactly what you request.

[Swift 3 Update] If you want a pure Swift solution. My (in progress) Scalar2D library has an extension on CGPath that creates a CGPath from an SVG path.

This solution is a straight conversion of an SVG path string to UIBezierPath. It worked great for my app where I had to draw the united states as a UIView.

https://github.com/ap4y/UIBezierPath-SVG

Oneliner that adds an SVG to an NSView:

addSubView(SVGParser.svg(FileParser.xml(~/Desktop/test.svg)))

SVGLib for swift: https://github.com/eonist/swift-utils

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!