2016-09-23 99 views
0
頁面

我從URL保存爲PDF文件,如果我保存單個網頁則是沒有問題的,但是當我保存整個PDF應用是越來越沒有任何日誌墜毀保存多個PDF

我的代碼

CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)url); 
NSString *pass = [_JSON objectForKey:@"pass"]; 
CGPDFDocumentUnlockWithPassword(document, [pass UTF8String]); 

int i = (int) CGPDFDocumentGetNumberOfPages(document); 
NSLog(@"Number Of pages: %d",i); 


//Create the pdf context 
for (int j = 0; j<i; j++) { 
    page = CGPDFDocumentGetPage(document, j+1); //Pages are numbered starting at 1 
    pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
    mutableData = CFDataCreateMutable(NULL, j); 
} 

//NSLog(@"w:%2.2f, h:%2.2f",pageRect.size.width, pageRect.size.height); 
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData); 
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL); 

if (CGPDFDocumentGetNumberOfPages(document) > 0) 
{ 
    //Draw the page onto the new context 
    //page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1 

    CGPDFContextBeginPage(pdfContext, NULL); 
    CGContextDrawPDFPage(pdfContext, page); 
    CGPDFContextEndPage(pdfContext); 
} 
else 
{ 
    NSLog(@"Failed to create the document"); 
} 

CGContextRelease(pdfContext); //Release before writing data to disk. 

//Write to disk 
[(__bridge NSData *)mutableData writeToFile:@"/Users/user/Desktop/sample.pdf" atomically:YES]; 

//CleanUP 
CGDataConsumerRelease(dataConsumer); 
CGPDFDocumentRelease(document); 
CFRelease(mutableData); 

我認爲問題是與循環,但不太瞭解這個模塊,我第一次嘗試這個。 Here is the output log which i could never understand and there is no log in console

回答

0

,我發現自己的答案與我的代碼擺弄 這裏是我的新工作的編碼

CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)url); 
NSString *pass = [_JSON objectForKey:@"pass"]; 
CGPDFDocumentUnlockWithPassword(document, [pass UTF8String]); 

int i = (int) CGPDFDocumentGetNumberOfPages(document); 
NSLog(@"Number Of pages: %d",i); 

//Create the pdf context 
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 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 
    //page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1 

    for (int j = 0; j < i; j++) { 
    page = CGPDFDocumentGetPage(document, j+1); 
    CGPDFContextBeginPage(pdfContext, NULL); 
    CGContextDrawPDFPage(pdfContext, page); 
    CGPDFContextEndPage(pdfContext); 
    } 
} 
else 
{ 
    NSLog(@"Failed to create the document"); 
} 
CGContextRelease(pdfContext); //Release before writing data to disk. 

//Write to disk 

NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
path = arr[0]; 
path = [path stringByAppendingPathComponent:@"sample.pdf"]; 

[(__bridge NSData *)mutableData writeToFile:path atomically:YES]; 


//CleanUP 
CGDataConsumerRelease(dataConsumer); 
CGPDFDocumentRelease(document); 
CFRelease(mutableData); 

我只是循環哪裏我應該循環只有我在做的部分,整個事情頁面創建部分。