我的应用程序中有几个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之后停止旋转,显示当前呈现的页面的标签也停止更新。有人知道怎么解决吗?
发布于 2014-03-08 09:08:03
出现此问题的原因是图表的系列部分是在openGLES中呈现的,因此不会被呈现为renderInContext:的一部分。
你有几个选项可以用来调查。第一种是在UIView上添加iOS7中的一些快照方法。如果您的应用程序只能被限制为iOS7,那么snapshotViewAfterScreenUpdates:将返回一个UIView,它是内容的快照。我认为下列(未经测试的)代码将起作用:
UIGraphicsBeginPDFPage();
UIView *pdfPage = [pd1.view snapshotViewAfterScreenUpdates:YES];
[pdfPage.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();在ShinobiControls的http://www.shinobicontrols.com/blog/posts/2014/02/24/taking-a-chart-snapshot-in-ios7博客上有关于这种方法的更多细节。
如果把你的应用程序限制在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上找到。
希望这能有所帮助
相同的
https://stackoverflow.com/questions/22259349
复制相似问题