2016-09-23 70 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); 

我只是循环哪里我应该循环只有我在做的部分,整个事情页面创建部分。