Maintain vector graphics when rendering CALayer in

2019-04-12 23:39发布

问题:

I am rendering a plot with many data points into a CALayer that is hosted by an NSView. All drawing operations done within drawInContext: of the CALayer are vector operations (CGPaths etc.).

Now I would like to export this as a PDF file. Using CGPDFContextCreateWithURL(), a PDF file is created, but it essentially just contains a screenshot of my plot, meaning a bitmap image of the the contents of the view.

But what I want is vector-based PDF. I know that CALayers render their content offscreen into a bitmap which is then shown on screen, but why is it not possible to render the very same content as a vector graphics instead?

Is there really no built-in way to achieve this? I must not be the only one needing this.

回答1:

You need to rewrite your code, the code that draws on the CALayer context should draw also on the PDF context, do not render the CALayer on PDF context because you will get the bitmap output.