简体   繁体   中英

Core plot graph is rendered as black box when converting into pdf

I my application used Core Plot to render graphs, and finally there is a need to create a pdf file, there should be this graph, but when it is saved to pdf file, instead of graph I see black box. The method I use to create a pdf:

UIGraphicsBeginPDFContextToFile(outputPath, CGRectZero, nil);

for(UIView *view in views) {
    UIGraphicsBeginPDFPageWithInfo(view.bounds, nil);
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();UIGraphicsBeginPDFContextToFile
    [view.layer renderInContext:pdfContext];
}

UIGraphicsEndPDFContext();

Could someone help how to make it render properly?

As a temporary solution before creating a pdf I convert graph to image and put it on the top of it as a result pdf is rendered properly, Is there any more elegant solution?

Edited: I applied the above advice the code looks like this:

        if([child isKindOfClass:[CPTGraphHostingView class]]) {
        CGContextTranslateCTM(pdfContext, child.center.x, child.center.y);
        CGAffineTransform transform = CGAffineTransformMakeRotation(0);
        transform.d = -1;

        CGContextConcatCTM(pdfContext, transform);
        CGContextTranslateCTM(pdfContext,
                              child.bounds.size.width * 0.05,
                              -child.bounds.size.height * 0.75);

        CPTLayer *layer = (CPTLayer *)((CPTGraphHostingView *)child).hostedGraph;

        [layer layoutAndRenderInContext:pdfContext];
    }

Result on UI: 在此处输入图片说明

and as a result is pdf:

在此处输入图片说明

Are you using any fills with partially transparent colors? The alpha channel doesn't render into PDF. This is a long-standing issue with the Apple PDF frameworks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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