将UIView渲染为PDF作为iPad上的矢量 - 有时渲染为位图,有时渲染为矢量 [英] Rendering a UIView into a PDF as vectors on an iPad - Sometimes renders as bitmap, sometimes as vectors

查看:82
本文介绍了将UIView渲染为PDF作为iPad上的矢量 - 有时渲染为位图,有时渲染为矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPad应用程序,我正在尝试从UIView生成PDF,而且几乎正常工作。

I have an iPad app and I'm trying to generate a PDF from a UIView and it's almost working perfectly.

代码非常简单如下:

UIGraphicsBeginPDFContextToFile( filename, bounds, nil );
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
[view.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();

这非常适用于一个奇怪的例外。如果在渲染为PDF之前视图已在屏幕上,则视图上的UILabel将作为精彩向量呈现给PDF。如果视图尚未显示在屏幕上(IE控制器是initWithNib等但尚未被推入导航控制器或其他任何东西),则文本将以'ipad'分辨率呈现为位图。

This works really well with one weird exception. If the view has been on screen before being rendered to PDF then the UILabels on the view are rendered to the PDF as wonderful vectors. If the view has not yet been on the screen (IE the controller was initWithNib etc but hasn't been pushed into a navigation controller or anything) then the text is rendered as a bitmap at 'ipad' resolution.

当我随后将其渲染为pdf上下文时,就像渲染到屏幕上的行为一样,将视图渲染为矢量。

It's like the act of getting rendered to the screen sets up the view to be rendered as vectors when I subsequently render it to a pdf context.

我可以在视图或图层或其他地方设置一些我可以调用的属性或属性来模仿这种行为,而不必在屏幕上显示视图吗?

Is there some method I can call or property I can set on the view or the layer or elsewhere to mimic this behaviour without having to show the view on screen?

是否与UIViewPrintFormatter有关?

Is it something to do with UIViewPrintFormatter?

推荐答案

唯一的方法我发现它使标签呈现矢量化是使用UILabel的子类与以下方法:

The only way I found to make it so labels are rendered vectorized is to use a subclass of UILabel with the following method:

/** Overriding this CALayer delegate method is the magic that allows us to draw a vector version of the label into the layer instead of the default unscalable ugly bitmap */
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    BOOL isPDF = !CGRectIsEmpty(UIGraphicsGetPDFContextBounds());
    if (!layer.shouldRasterize && isPDF)
        [self drawRect:self.bounds]; // draw unrasterized
    else
        [super drawLayer:layer inContext:ctx];
}

这样做对我来说很简单:标签没有被标注,可在生成的PDF中选择查看,并在渲染到屏幕时表现正常。

That does the trick for me: labels are unrasterized and selectable in the resulting PDF view, and behave normally when rendered to the screen.

这篇关于将UIView渲染为PDF作为iPad上的矢量 - 有时渲染为位图,有时渲染为矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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