如何将一个UIView转换成现有的PDF格式?

时间:2021-11-24 19:45:02

I am creating a PDF with labels, tables, and charts. I have no problem drawing images and text, but I have PDF charts that are custom UIViews.

我正在创建一个带有标签、表格和图表的PDF。我没有问题绘图和文本,但我有PDF图表,是自定义的uiview。

The current context is set initially for drawing the PDF, and most of the questions here are relating to that. 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. i.e. I want to add a UIView that contains a pie chart to a PDF I am creating.

当前的上下文最初是用来绘制PDF的,而这里的大多数问题都与此相关。但我说的是,一旦你已经画了一个PDF,你如何添加一个额外的视图,就像你会添加一行文字或图像一样。也就是说,我想添加一个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. The manager is a simple custom class I wrote to manage it.

PieChartView来自一个开源的github库。管理器是我用来管理它的一个简单的自定义类。

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. This isn't surprising, since its layer doesn't contain information about the frame.

这个结果在饼图中填充了整个页面的PDF。这并不奇怪,因为它的层不包含关于框架的信息。

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.

我想我应该使用CGContextTranslateCTM和CGContextScaleCTM来重新定位,但是我不确定是否有更简单的方法,而且我也不知道如何使用它们来得到我想要的。

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?

这将在页面的左上方绘制该饼图,大小与预期相同。我要怎么得到它?框架包含了我想要它的原点,那么为什么把它放在左上角?

1 个解决方案

#1


0  

It appears as if the custom UIView which renders inside the pdf will always start from (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将始终从(0,0)开始。您可以在pdf页面的整个框架中添加您的pie视图,并在您的UIView的drawRect函数中在一个自定义较小的框架上绘制实际的饼图,并使剩余的区域透明。

#1


0  

It appears as if the custom UIView which renders inside the pdf will always start from (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将始终从(0,0)开始。您可以在pdf页面的整个框架中添加您的pie视图,并在您的UIView的drawRect函数中在一个自定义较小的框架上绘制实际的饼图,并使剩余的区域透明。