2017-04-12 124 views
1

我渲染PDF用下面的代碼作爲測試:CoreGraphics中:無法呈現彩色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; 
} 

我希望框是青色,但輸出爲黑色。

回答

0

CGPDFContextBeginPage(pdfContext, nil);移動到色彩空間以上。我認爲你的色彩空間修改正在悄悄丟棄,因爲你還沒有開始一個頁面。謝謝,CoreGraphics!