我的应用程序使用Core Plot来渲染图形,最后需要创建一个pdf文件,应该有这个图形,但是当它保存到pdf文件时,我看到的不是图形,而是黑框。 我用来创建pdf的方法:
UIGraphicsBeginPDFContextToFile(outputPath, CGRectZero, nil);
for(UIView *view in views) {
UIGraphicsBeginPDFPageWithInfo(view.bounds, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();UIGraphicsBeginPDFContextToFile
[view.layer renderInContext:pdfContext];
}
UIGraphicsEndPDFContext();
有人可以帮助如何使其正确渲染吗?
作为创建 pdf 之前的临时解决方案,我将图形转换为图像并将其放在其顶部,结果 pdf 正确呈现,是否有更优雅的解决方案?
编辑: 我应用了上述建议,代码如下所示:
if([child isKindOfClass:[CPTGraphHostingView class]]) {
CGContextTranslateCTM(pdfContext, child.center.x, child.center.y);
CGAffineTransform transform = CGAffineTransformMakeRotation(0);
transform.d = -1;
CGContextConcatCTM(pdfContext, transform);
CGContextTranslateCTM(pdfContext,
child.bounds.size.width * 0.05,
-child.bounds.size.height * 0.75);
CPTLayer *layer = (CPTLayer *)((CPTGraphHostingView *)child).hostedGraph;
[layer layoutAndRenderInContext:pdfContext];
}
结果是 pdf:
最佳答案
您是否使用了部分透明颜色的填充? Alpha channel 不会渲染为 PDF。这是 Apple PDF 框架长期存在的问题。
关于ios - 核心图转换为pdf时呈现为黑框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353990/