Thanks in advance!
The signature of the class method is (NSString *)svgPathFromCGPath:(CGPathRef)aPath;
[Update: Added code snippet below]< /p>
#import "SVGPathGenerator.h"
…
CGMutablePathRef pathToBuild = CGPathCreateMutable();< br />CGPathMoveToPoint(pathToBuild, nil, 10, 10);
CGPathAddQuadCurveToPoint(pathToBuild, nil, 20, 0, 30, 40);
CGPathAddCurveToPoint(pathToBuild, nil, 20, 40, 40, 30 , 100, 90);
CGPathAddLineToPoint(pathToBuild, nil, 50, 100);
NSString* aDAttribute = [SVGPathGenerator svgPathFromCGPath:pathToBuild];
NSLog(@"%@ ",aDAttribute);
Output:
M10.0 10.0Q20.0 0.0 30.0 40.0C20.0 40.0 40.0 30.0 100.0 90.0L50.0 100.0
Note, if you add Arc, the output will not have a close correspondence, because the description of the arc in SVG and the arc in Quartz are completely different.
If you want to output it to a simple SVG file, then you can do something like Things:
CGRect boundingBox = CGPathGetPathBoundingBox(pathToBuild);
NSString* sv gAsString = [NSString stringWithFormat:@" \
\
",
boundingBox.origin.x, boundingBox.origin.y, boundingBox.size.width, boundingBox.size.height, boundingBox. size.height, boundingBox.size.width, aDAttribute
];
NSData* utf8Data = [svgAsString dataUsingEncoding:NSUTF8StringEncoding];
[utf8Data writeToFile:pathToFile atomically:YES];
(I did not compile this code).
I used CGPath and CAShapeLayer to make a drawing (drawing screen), and now my client wants To output images that can be expanded, I found SVG Kit SVG Kit, but I don’t know how to use this library. I didn’t find any examples in the library examples! Does anyone know how to convert CGPath to SVG using this library or can give me a tutorial?
Thanks in advance!
You can start by viewing at this file on GitHub. It has a routine to convert CGPathRef to the d attribute of an SVG path. I wrote it.
The signature of the class method is (NSString *)svgPathFromCGPath:(CGPathRef)aPath;
[Update: Added code snippet below]
#import "SVGPathGenerator.h"
…
CGMutablePathRef pathToBuild = CGPathCreateMutable();
CGPathMoveToPoint(pathToBuild, nil, 10, 10 );
CGPathAddQuadCurveToPoint(pathToBuild, nil, 20, 0, 30, 40);
CGPathAddCurveToPoint(pathToBuild, nil, 20, 40, 40, 30, 100, 90);
CGPathAddLineToPoint( pathToBuild, nil, 50, 100);
NSString* aDAttribute = [SVGPathGenerator svgPathFromCGPath:pathToBuild];
NSLog(@"%@",aDAttribute);
Output:
M10.0 10.0Q20.0 0.0 30.0 40.0C20.0 40.0 40.0 30.0 100.0 90.0L50.0 100.0
Note that if arcs are added, the output will not have a close correspondence, because SVG The description of the arc in Quartz and the arc in Quartz are completely different.
If you want to output it to a simple SVG file, then you can do something similar:
p>
CGRect boundingBox = CGPathGetPathBoundingBox(pathToBuild);
NSString* svgAsString = [NSString stringWithFormat:@" \
\
",
boundingBox.origin.x, boundingBox.origin.y, boundingBox.size.width, boundingBox.size.height, boundingBox.size.height, boundingBox.size.width, aDAttribute
];< br />
NSData* utf8Data = [svgAsString dataUsingEncoding:NSUTF8StringEncoding];
[utf8Data writeToFile:pathToFile atomically:YES];
(I did not compile this code). p>