如何从 NSData 源编写 PDF 文件
我有一个从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说得对。
如果 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...)
这有点转移注意力。正在绘制路径,但没有笔划或填充。我上面问题中的代码是合理的并且按预期工作。
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.