将shinobi图表打印成PDF [英] Printing shinobi chart into PDF

查看:100
本文介绍了将shinobi图表打印成PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have several shinobicharts in my App that I want to print into a PDF file. Everything else, like normal views, labels and images work fine, even the grid, legend and gridlabels are displayed. The only thing missing are the series. So basically I get an empty chart printed into the PDF file.

我按如下方式打印PDF:

I print the PDF as follows:

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中绘制漂亮的图表,而不会丢失该系列. 数组包含应显示的数据.

The exact same code from PDF1PageViewController draws beautiful charts in a normal viewController, not missing the series. The arrays contain the data which should be displayed.

这段代码对我有用:

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之后活动轮停止旋转,并且显示正在渲染的当前页面的标签也停止更新.有人知道如何解决这个问题吗?

Although the activity wheel stops spinning after drawViewHierarchyInRect and the label displaying the current page being rendered also stops updating. Anyone knows how to fix this?

推荐答案

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

The reason that you're having this problem is that the series part of the charts are rendered in openGLES, and therefore don't get rendered as part of renderInContext:.

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

You have a couple of options which you can investigate using. the first is the addition of some snapshotting methods on UIView in iOS7. If you're app can be restricted to iOS7 only, then snapshotViewAfterScreenUpdates: will return you a UIView which is a snapshot of the content. I think that the following (untested) code will work:

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

ShinobiControls博客上的

There are more details on this approach on the ShinobiControls blog at http://www.shinobicontrols.com/blog/posts/2014/02/24/taking-a-chart-snapshot-in-ios7

如果不能将应用程序限制为iOS7,那么您仍然可以实现所需的结果,但是要复杂一些.再次幸运的是,在ShinobiControls博客上有一篇博客文章(

If restricting your app to iOS7 isn't an option then you can still achieve the result you want, but it is a little more complicated. Luckily, again, there is a blog post on the ShinobiControls blog (http://www.shinobicontrols.com/blog/posts/2012/03/26/taking-a-shinobichart-screenshot-from-your-app) which describes how to create a UIImage from a chart. This could easily be adapted to render into your PDF context, rather than the image context created in the post. There is an additional code snippet to accompany the post, available on github: https://github.com/ShinobiControls/charts-snippets/tree/master/iOS/objective-c/screenshot.

希望这会有所帮助

山姆

这篇关于将shinobi图表打印成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆