3
我試圖將pdf頁面繪製到pdf上下文中,然後將其保存到磁盤。我似乎無法弄清楚什麼是錯的。有人能給我幾個指點。謝謝。使用Quartz將PDF文檔保存到磁盤
- (void)testQuartz:(NSData *)pdfDocumentData
{
//Create the pdf document reference
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);
CGDataProviderRelease(dataProvider); //Release the data provider
//Create the pdf context
CGPDFPageRef page = CGPDFDocumentGetPage(document, 0);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);
if (CGPDFDocumentGetNumberOfPages(document) > 0)
{
//Draw the page onto the new context
page = CGPDFDocumentGetPage(document, 0);
CGContextDrawPDFPage(pdfContext, page);
}
else
{
NSLog(@"Failed to create the document");
}
//Write to disk
[(NSMutableData *)mutableData writeToFile:@"/Users/David/Desktop/test.pdf" atomically:YES];
//Clean up
CGDataConsumerRelease(dataConsumer);
CFRelease(mutableData);
CGPDFDocumentRelease(document);
}
那麼有什麼不工作?它不是在編譯?建築時是否收到錯誤或警告?你的日誌消息是輸出嗎? – Abizern 2010-09-23 17:12:21
代碼執行時,我不會收到任何警告或錯誤。我知道有什麼地方是錯誤的,因爲PDF文件無法打開。 – David 2010-09-23 17:26:22