我用CGPath和CAShapeLayer做了一个绘画(绘图画面),现在我的客户想要输出图像可扩展,我找到了SVG Kit SVG Kit,但我不知道如何使用这个库.我没有在库示例中找到和示例!有谁知道如何使用这个
提前致谢 !
您可以从查看 at this file on GitHub开始.它具有将CGPathRef转换为SVG路径的d属性的例程.我写的.类方法的签名是(NSString *)svgPathFromCGPath:(CGPathRef)aPath;
[更新:在下面添加了代码段]
#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);
输出:
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
注意,如果添加弧,输出将没有紧密的对应关系,因为SVG中的弧和Quartz中的弧的描述完全不同.
如果你想将它输出到一个简单的SVG文件,那么你可以做类似的事情:
CGRect boundingBox = CGPathGetPathBoundingBox(pathToBuild); NSString* svgAsString = [NSString stringWithFormat:@" <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> \ <svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewport-fill=\"none\" viewBox=\"%lf, %lf, %lf, %lf\" version=\"1.1\" height=\"%lf\" width=\"%lf\" > \ <path fill=\"none\" stroke=\"green\" d=\"%@\" /> \ </svg>", 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];
(我没有编译这段代码).