简体   繁体   English

如何转换单个UIView,以便可以在现有PDF中绘制它?

[英]How do you convert a single UIView so that you can draw it within an existing PDF?

I am creating a PDF with labels, tables, and charts. 我正在创建带有标签,表格和图表的PDF。 I have no problem drawing images and text, but I have PDF charts that are custom UIViews . 我在绘制图像和文本时没有问题,但是我具有自定义UIViews PDF图表。

The current context is set initially for drawing the PDF, and most of the questions here are relating to that. 最初为绘制PDF设置了当前上下文,这里的大多数问题都与此有关。 But I am talking about, once you're already drawing a PDF, how do you add a single additional view like you would add a line of text or an image. 但是我说的是,一旦您已经绘制了PDF,如何添加单个视图,就像添加一行文本或图像一样。 ie I want to add a UIView that contains a pie chart to a PDF I am creating. 即我想添加一个包含饼图的UIView到我正在创建的PDF。

I tried converting the view to an image, but it ends up pretty low resolution and you can seed the pixelation, whereas if you draw it directly it uses vector graphics and comes out much crisper. 我尝试将视图转换为图像,但最终分辨率很低,您可以播种像素化,而如果直接绘制,则使用矢量图形,效果更加清晰。

PieChartView is from an open source github library. PieChartView来自开源github库。 The manager is a simple custom class I wrote to manage it. 管理器是我编写的用于管理它的简单自定义类。

CGRect frame = CGRectMake(PAGE_WIDTH/2 - pieWidth/2, origin.y, pieWidth, pieHeight);
PieChartView* pie = [[PieChartView alloc] initWithFrame:frame];
PieChartManager* pieManager = [[BPPieChartManager alloc] initWithData:data];
pie.delegate = pieManager;
pie.datasource = pieManager;
[pie.layer drawInContext:UIGraphicsGetCurrentContext()];

This results in the pie chart filling the entire page of the PDF. 这将导致饼图填充PDF的整个页面。 This isn't surprising, since its layer doesn't contain information about the frame. 这并不奇怪,因为其层不包含有关框架的信息。

I guess I am supposed to use CGContextTranslateCTM and CGContextScaleCTM to reposition, but I'm not sure if there isn't a simpler way, plus I'm not sure how to use them to get what I want. 我猜我应该使用CGContextTranslateCTMCGContextScaleCTM来重新定位,但是我不确定是否有更简单的方法,而且我不确定如何使用它们来获得我想要的东西。

Thanks for your help 谢谢你的帮助

EDIT: I tried just drawing the pie this way: 编辑:我试图只是这样绘制派:

[pie drawRect: frame];

That draws the pie in the top left of the page, in the size that's expected. 这会将饼图以预期的大小绘制在页面的左上方。 How do I get it where I want it? 如何在需要的地方得到它? The frame contains the origin of where I want it, so why is it putting it in the top left? 框架包含我想要的位置的原点,那么为什么将其放在左上角?

It appears as if the custom UIView which renders inside the pdf will always start from (0,0). 似乎在pdf内呈现的自定义UIView总是从(0,0)开始。 You can add the your pie view for the entire frame of the pdf page and draw the actual pie chart at a custom smaller frame in your UIView's drawRect function and make the remaining area transparent. 您可以在pdf页面的整个框架中添加饼图视图,并在UIView的drawRect函数中的一个自定义的较小框架上绘制实际的饼图,并使其余区域透明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM