【问题标题】:How to save CGPath in svg or pdf? [closed]如何将 CGPath 保存为 svg 或 pdf? [关闭]
【发布时间】:2013-11-07 13:49:51
【问题描述】:

嗨,我还是 mac 和 Objective-c 的新手。

我搜索了几个小时来回答我的问题,但我没有得到任何结果。

我想绘制一些简单的 2D 路径/对象并将其保存到 svg 或 pdf 文件中。

我的计划是创建一个CGPath(我已经知道该怎么做),然后将其导出到我磁盘上的svg 或pdf 文件。

如果有人有某种代码 sn-p 或教程,我将非常感谢。

希望,荷兰。

【问题讨论】:

  • 这个有价值的问题怎么跑题了。

标签: objective-c macos cocoa svg cgpath


【解决方案1】:

【讨论】:

  • 感谢您的提示。帮助我得到正确的东西。
【解决方案2】:

我使用了user1118321的Tipp,在Core Graphics中搜索。

遗憾的是,它仍然花了我很多时间,不知何故我无法将现有路径添加到上下文中......不知何故我无法抚摸它。

就我而言,这没问题,因为我想画一个新的。 Core Graphics 确实提供了类似于 CGPaths 在 CGContext 中绘制功能的功能。

这是我用来创建 pdf 文件并在其中绘制新路径的代码:

-(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);
}

【讨论】:

    猜你喜欢
    • 2012-09-19
    • 2014-02-06
    • 1970-01-01
    • 2018-07-17
    • 2012-08-16
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多