保存多个pdf页面

时间:2016-09-23 14:45:35

标签: objective-c pdf

我正在从url保存一个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

1 个答案:

答案 0 :(得分:0)

我通过摆弄我的代码找到了答案 这是我的新工作编码

{{1}}

我只是循环整个事情,因为我应该只循环我正在做页面创建部分的部分。