2013-10-11 54 views
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; 
} 

回答

0

試試這個:

檢查此代碼..它會遍歷爲&寫每一頁pdfContext所有頁面。然後保存該文件。

if (dict == nil) return nil; 

    NSString *tempName = [dict objectForKey:@"name"]; 

    CGPDFDocumentRef document = [self newPdfDocumentFromBase64DataString:[dict objectForKey:@"base64"]]; 

    CGPDFPageRef page; 

    CFURLRef pdfURLOutput = (CFURLRef)[[NSURL alloc] initFileURLWithPath:YOUR_OUTPUT_FULL_FILE_PATH]; 

    CGContextRef pdfContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);; 

    CGRect pageRect; 

    for (int i=1; i<=CGPDFDocumentGetNumberOfPages(document); i++) 
    { 
     page = CGPDFDocumentGetPage(document, i); 

     pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 

     CGContextBeginPage(pdfContext, &pageRect); 

     CGContextDrawPDFPage(pdfContext, page); 

     CGContextEndPage(pdfContext); 
    } 

    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; 
+0

我認爲你有權利。因爲如果我現在從1改爲3,那麼我得到的是第3頁。但是我們怎樣才能寫出所有的頁面。這就是問題所在。無論如何感謝您的幫助。 – user2754113

+0

如果它對您有幫助,請將其標記爲答案。 –

+0

@ user2754113看到新的答案。這正是你想要的。 –