使用下面的代码,我成功编写了 pdf,但不是所有文件!我可以看到文件是 3 页,但只有一页在写。有谁知道为什么?
(PresentationItem*)presentationItemFromDictionary:(NSDictionary*)dict {
if (dict == nil) return nil;
CGPDFDocumentRef document = [self newPdfDocumentFromBase64DataString:[dict objectForKey:@"base64"]];
NSString *tempName = [dict objectForKey:@"name"];
// Create the pdf context
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
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
CGPDFContextBeginPage(pdfContext, NULL);
CGContextDrawPDFPage(pdfContext, page);
CGPDFContextEndPage(pdfContext);
}
else{
NSLog(@"Failed to create the document");
}
CGContextRelease(pdfContext);
// Write to disk
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/%@.pdf", docDir,tempName];
[(__bridge NSData *)mutableData writeToFile:fileName atomically:YES];
PresentationItem *item = [[PresentationItem alloc] init];
[item setTitle:[dict objectForKey:@"name"]];
[item setDocument:document];
CGPDFDocumentRelease(document);
CGDataConsumerRelease(dataConsumer);
CFRelease(mutableData);
return item;
}