如何从 NSData 源编写 PDF 文件

发布于 2024-12-07 10:32:41 字数 1459 浏览 1 评论 0原文

我有一个从 CGPathRef 返回 NSData 的方法,如下所示...

+ (NSData *) createPDFDataWithCGPath: (CGPathRef) path mediaBox: (CGRect) mediaBox
{
  CFMutableDataRef data = NULL;
  if (path) {
    CFAllocatorRef allocator = NULL;
    data = CFDataCreateMutable(allocator, 0);
    CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(data);
    CGContextRef context = CGPDFContextCreate(consumer, &mediaBox, NULL);
    CFTypeRef keys[1] = { kCGPDFContextMediaBox };
    CFTypeRef values[1] = { CFDataCreate(allocator, (const UInt8 *)&mediaBox, sizeof(CGRect)) };
    CFDictionaryRef pageInfo = CFDictionaryCreate(allocator, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

    CGPDFContextBeginPage(context, pageInfo);
    CGContextScaleCTM(context, 1, -1);
    CGContextTranslateCTM(context, 0, -mediaBox.size.height);
    CGContextAddPath(context, path);
    CGContextSetRGBFillColor(context, 0, 0, 0, 1);
    CGContextFillPath(context);
    CGPDFContextEndPage(context);

    CFRelease(pageInfo);
    CFRelease(values[0]);
    CGPDFContextClose(context);
    CGContextRelease(context);
    CGDataConsumerRelease(consumer);
  }
  return (NSData *)data;
}

当我尝试使用它并从数据写入 PDF 文件时,我得到了正确大小的文件,但路径没有绘制到 PDF 中...它是基本上是一个空文档。

像这样编写文件就足够了……

[maskData writeToFile: DOCUMENTS_PATH_WITH_STRING(@"maskData.pdf") atomically: YES];

还是需要克服更多困难才能将其编写为 PDF?

I have a method that returns NSData from a CGPathRef like so ...

+ (NSData *) createPDFDataWithCGPath: (CGPathRef) path mediaBox: (CGRect) mediaBox
{
  CFMutableDataRef data = NULL;
  if (path) {
    CFAllocatorRef allocator = NULL;
    data = CFDataCreateMutable(allocator, 0);
    CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(data);
    CGContextRef context = CGPDFContextCreate(consumer, &mediaBox, NULL);
    CFTypeRef keys[1] = { kCGPDFContextMediaBox };
    CFTypeRef values[1] = { CFDataCreate(allocator, (const UInt8 *)&mediaBox, sizeof(CGRect)) };
    CFDictionaryRef pageInfo = CFDictionaryCreate(allocator, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

    CGPDFContextBeginPage(context, pageInfo);
    CGContextScaleCTM(context, 1, -1);
    CGContextTranslateCTM(context, 0, -mediaBox.size.height);
    CGContextAddPath(context, path);
    CGContextSetRGBFillColor(context, 0, 0, 0, 1);
    CGContextFillPath(context);
    CGPDFContextEndPage(context);

    CFRelease(pageInfo);
    CFRelease(values[0]);
    CGPDFContextClose(context);
    CGContextRelease(context);
    CGDataConsumerRelease(consumer);
  }
  return (NSData *)data;
}

When I attempt to use this and write a PDF file from the data I get my file at the correct size but the paths are not drawn into the PDF ... it's an empty document basically.

Is it enough to just write the file like so ...

[maskData writeToFile: DOCUMENTS_PATH_WITH_STRING(@"maskData.pdf") atomically: YES];

or are their more hoops to jump though to write it as a PDF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠佳期 2024-12-14 10:32:42

你说得对。
如果 NSData 对象中有数据,那么您可以将其写入磁盘。但您应该添加正确的扩展名(在您的情况下为.pdf)。
它适用于一切(视频、图片等......)

You're right.
If you have data in an NSData object then you can just write it on the disk. But you should add the correct extension (in your case .pdf).
It works for everything (videos, pictures, etc...)

小苏打饼 2024-12-14 10:32:42

这有点转移注意力。正在绘制路径,但没有笔划或填充。我上面问题中的代码是合理的并且按预期工作。

This was a bit of a red herring. The paths were being drawn but had no strokes or fills. The code in my question above is sound and works as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文