2

我的应用程序中有几个 shinobicharts,我想打印成 PDF 文件。其他一切,如普通视图、标签和图像都可以正常工作,甚至显示网格、图例和网格标签。唯一缺少的是系列。所以基本上我得到一个打印到 PDF 文件中的空图表。

我按如下方式打印 PDF:

NSMutableData * pdfData=[NSMutableData data];
PDFPage1ViewController *pdf1 = [self.storyboard instantiateViewControllerWithIdentifier:@"PDF1"];
pdf1.array1 = array1;
pdf1.array2 = array2;
pdf1.array3 = array3;
pdf1.array4 = array4;
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero,nil);
CGContextRef pdfContext=UIGraphicsGetCurrentContext();
UIGraphicsBeginPDFPage();
[pdf1.view.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();

来自 PDF1PageViewController 的完全相同的代码在普通的 viewController 中绘制了漂亮的图表,而不是缺少系列。数组包含应显示的数据。

[编辑]

这段代码为我做了:

UIGraphicsBeginImageContextWithOptions(pdf1.view.bounds.size, NO, 0.0);
[pdf1.view drawViewHierarchyInRect:pdf1.view.bounds afterScreenUpdates:YES];
UIImage *pdf1Image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *pdf1ImageView = [[UIImageView alloc] initWithImage:pdf1Image];
[pdf1ImageView.layer renderInContext:pdfContext];

尽管活动轮在之后停止旋转,drawViewHierarchyInRect并且显示当前正在呈现的页面的标签也停止更新。任何人都知道如何解决这个问题?

4

1 回答 1

1

您遇到此问题的原因是图表的系列部分在 openGLES 中呈现,因此不会作为renderInContext:.

您可以使用几个选项进行调查。UIView首先是在 iOS7中添加了一些快照方法。如果您的应用程序只能限制为 iOS7,那么snapshotViewAfterScreenUpdates:将返回一个UIView内容快照。我认为以下(未经测试的)代码将起作用:

UIGraphicsBeginPDFPage();
UIView *pdfPage = [pd1.view snapshotViewAfterScreenUpdates:YES];
[pdfPage.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();

在http://www.shinobicontrols.com/blog/posts/2014/02/24/taking-a-chart-snapshot-in-ios7的 ShinobiControls 博客上有关于这种方法的更多详细信息

如果不能将您的应用程序限制在 iOS7 上,那么您仍然可以实现您想要的结果,但它有点复杂。幸运的是,ShinobiControls 博客 ( http://www.shinobicontrols.com/blog/posts/2012/03/26/taking-a-shinobichart-screenshot-from-your-app ) 上有一篇博文描述了如何UIImage从图表创建。这可以很容易地适应呈现到您的 PDF 上下文中,而不是在帖子中创建的图像上下文中。这篇文章附带了一个额外的代码片段,可在 github 上找到:https ://github.com/ShinobiControls/charts-snippets/tree/master/iOS/objective-c/screenshot 。

希望这可以帮助

山姆

于 2014-03-08T09:08:03.423 回答