0
隨着下面的代碼,我成功地寫了pdf,但不是所有的文件!我可以看到文檔是3頁,但只有一頁正在書寫。有誰知道爲什麼?IOS writeToFile只寫入文件的一部分
(PresentationItem *)presentationItemFromDictionary:(NSDictionary的*)字典{
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;
}
我認爲你有權利。因爲如果我現在從1改爲3,那麼我得到的是第3頁。但是我們怎樣才能寫出所有的頁面。這就是問題所在。無論如何感謝您的幫助。 – user2754113
如果它對您有幫助,請將其標記爲答案。 –
@ user2754113看到新的答案。這正是你想要的。 –