CoreGraphics:无法呈现彩色PDF

时间:2017-04-12 17:19:26

标签: core-graphics quartz-2d

我正在使用以下代码渲染PDF作为测试:

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>

int main(int argc, const char * argv[]) {
  @autoreleasepool {

    NSURL* theFile = [NSURL fileURLWithPath:@"test.pdf"];

    CGContextRef pdfContext = CGPDFContextCreateWithURL((CFURLRef) theFile, nil, nil);

    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    assert(CGColorSpaceGetNumberOfComponents(rgb) == 3);
    CGContextSetFillColorSpace(pdfContext,rgb);
    CGContextSetStrokeColorSpace(pdfContext, rgb);
    CGColorSpaceRelease(rgb);

    CGPDFContextBeginPage(pdfContext, nil);

    CGFloat rgba[4] = {0.0,1.0,1.0,1.0};
    CGContextSetFillColor(pdfContext, rgba);
    CGContextFillRect(pdfContext, CGRectMake(100, 100, 100, 100));

    CGPDFContextEndPage (pdfContext);
    CGContextRelease (pdfContext);
  }
  return 0;
}

我希望盒子是青色的,但输出是黑色的。

1 个答案:

答案 0 :(得分:0)

CGPDFContextBeginPage(pdfContext, nil);移到颜色空间之上。我认为你的色彩空间修改正在被悄然丢弃,因为你还没有开始一个页面。谢谢,CoreGraphics!

相关问题