Drawing with CGPath to SVG output
You could start with looking at this file on GitHub. It has a routine to convert a CGPathRef to the d attribute of an SVG Path. I wrote it.
The class method's signature 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);
Outputs:
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, the output will not have a close correspondence if you add arcs, as arcs in SVG and arcs in Quartz are described quite differently.
If you want to output it to a simple SVG file, well then you could do something like:
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];
(I have not compiled this code).
Convert CAShapelayers to SVG
A shape layer contains a CGPath. I searched on "convert CGPath to SVG" and found this (rather old) post that demonstrates converting a CGPath to an SVG:
https://stackoverflow.com/a/22213852/205185
How to save CGPath in svg or pdf?
You can create a CGPDFContext
and draw into it using Core Graphics commands (like CGPath
stuff).
Convert UIView to SVG format
I'm worried that you don't understand what SVG output is. SVG is a description of a path or paths. So if your view is just some random view that you hope to get "magicked" over to a bunch of paths, I think you are in trouble. If it is an image, or some picture that someone drew, look in another direction.
If, however, you have paths, there are answers like this that show how to convert CGPaths to an SVG view:
https://stackoverflow.com/a/22213852/793607
Signature view iOS to svg format
For converting the image into svg
for that you have bezier path,not need to save into UIImage
.you need to convert it directly to an SVG
path string, and then insert that SVG
path into an SVG document.
Here is the answer for convert CGPath
to SVG
May this helps lot.
UILabel to SVG/vector?
Take a look at the "Getting Glyph Data" section in Apple's CTFont docs here: https://developer.apple.com/documentation/coretext/ctfont-q6r
A quick search for CTFontCreatePathForGlyph swift example
should get you practical example code to work with.
Output in SVG format
As said in comment, just output the svg code you need in some I/O stream:
int main()
{
int a = 5;
std::cout << "<svg xmlns=\"http://www.w3.org/2000/svg\" width='100' height='100'>\n";
std::cout << "<text x='10' y='20'>" << a << "</text>\n";
std::cout << "</svg>\n";
}
Then compile:
$ g++ a.cpp
and run:
$ a.out > myfile.svg
How to import/parse SVG into UIBezierpaths, NSBezierpaths, CGPaths?
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/
Related Topics
How to Group Array of Objects by Date in Swift
Core Data Predicate:Unimplemented SQL Generation for Predicate
Apple Watch - Only Getting Data If App on Phone Is Active
iOS 7 When Rotating View in Tab Bar, Right Side of View Is Not Clickable
Array of Multiple Url's Nsfilemanager Swift
How to Properly Remove Node When Out of Screen Bounds
Add Buttons to Uipickerview - Swift 1.2
Seed and Maintain Cloudkit Public Database Without Requiring Icloud Login
Running Nsurlsession Completion Handler on Main Thread
How to Make a Mkannotationview Touch Sensitive
Error Sending Archive to App Store Connect
Uploads Using Backgroundsessionconfiguration and Nsurlsessionuploadtask Cause App to Crash
Will Push Notification Still Work After Ownership Transfer in Itunesconnect
Xcode Error:Distill Failed for Unknown Reasons