将UIView渲染为PDF时没有背景

时间:2014-06-02 16:29:02

标签: ios objective-c pdf uiview render

在我的iPad应用中,我使用以下方法将UIView呈现为PDF。

+ (NSData*)createPDFfromUIView:(UIView*)aView
{
    NSMutableData *pdfData = [NSMutableData data];

    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();

    [aView.layer renderInContext:pdfContext];

    UIGraphicsEndPDFContext();

    return pdfData;
}

这很好用。我的问题是所有内容都正确呈现(所有子视图),但视图背景始终是白色的。如果我希望视图的背景也能正确呈现,我还需要改变什么?

1 个答案:

答案 0 :(得分:0)

如果视图具有有效的bg,则将其呈现为pdf:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //bg
    self.view.backgroundColor = [UIColor redColor];

    //make pdf
    NSData *pdf = [ViewController createPDFfromUIView:self.view];
    [pdf writeToFile:@"/Users/dominik/t.pdf" atomically:NO];

}

+ (NSData*)createPDFfromUIView:(UIView*)aView
{
        //question source code (see above)
}
@end

所以:设置backgroundColor