Question

Hi, i am still new at mac and objective-c.

I searched a few hours to answer my question, but i did not get anywhere.

I want to draw some simple 2D path / object and save it to a svg or pdf file.

My plan was to create a CGPath (i already know how to do that) and afterwards export it to svg or pdf file on my disk.

I would be very thankful if anyone has some kind of code snippet or tutorial, where this is explained.

Hopefully, NL.

Was it helpful?

OTHER TIPS

I used the Tipp of user1118321 and searched in the Core Graphics.

Sadly it still took me a lot of time and somehow i can not add an existing path to the context ... somehow i can't get it stroked.

In my case this is no problem, since i want to draw a new one. Core Graphics does offer functions similar to CGPaths drawing functions in CGContext.

This is my code i use to create a pdf file and draw a new path in it:

-(void) createPDFwithSize:(CGRect) size andFilename: (const char *) filename;
{
    CGContextRef pdfContext;
    CFStringRef path;
    CFURLRef url;
    CFDataRef boxData = NULL;
    CFMutableDictionaryRef myDictionary = NULL;
    CFMutableDictionaryRef pageDictionary = NULL;

    path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
    url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
    CFRelease (path);
    myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
    pdfContext = CGPDFContextCreateWithURL (url, &size, myDictionary);
    CFRelease(myDictionary);
    CFRelease(url);
    pageDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    boxData = CFDataCreate(NULL,(const UInt8 *)&size, sizeof (CGRect));
    CFDictionarySetValue(pageDictionary, kCGPDFContextMediaBox, boxData);
    CGPDFContextBeginPage (pdfContext, pageDictionary);


    // -----------------------------------------------------------------------------------------------------------------
    // Draw stuff ...

    CGContextSetLineWidth(pdfContext, 1);
    CGContextSetStrokeColorWithColor(pdfContext, CGColorCreateGenericRGB(0, 0, 0, 1));

    CGContextBeginPath(pdfContext);

    CGContextMoveToPoint(pdfContext, 100, 100);
    CGContextAddLineToPoint(pdfContext, 100, 150);
    CGContextAddLineToPoint(pdfContext, 125, 175);
    CGContextAddLineToPoint(pdfContext, 150, 150);
    CGContextAddLineToPoint(pdfContext, 150, 100);
    CGContextAddLineToPoint(pdfContext, 100, 150);
    CGContextAddLineToPoint(pdfContext, 150, 150);
    CGContextAddLineToPoint(pdfContext, 100, 100);
    CGContextAddLineToPoint(pdfContext, 150, 100);

    CGContextStrokePath(pdfContext); // don't forget this
    CGContextClosePath(pdfContext);

    // -----------------------------------------------------------------------------------------------------------------

    CGPDFContextEndPage (pdfContext);
    CGContextRelease (pdfContext);
    CFRelease(pageDictionary);
    CFRelease(boxData);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top