iOS:阅读PDF文件的内容

时间:2013-05-23 19:30:55

标签: ios pdf

通过转到Quartz 2D编程指南和SO上的一些帖子,我拼凑了一些代码来读取PDF文件的内容。我认为,它在PDF扫描仪部分失败了。当我去发布扫描仪时,应用程序崩溃时出现此错误:

avi(2282,0x3de7bb88) malloc: *** error for object 0x693f7c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

我通过电子邮件附件打开PDF文件。我知道它至少可以识别文件,因为我可以获得页数。有人可以查看下面的代码,看看我的错误发生在哪里?感谢。

- (void) readPDFFile:(NSURL *)PDFURL {

    //make a pdf document and point it to the url of our pdf file
    CGPDFDocumentRef pdfDocument;
    pdfDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)(PDFURL));

    //release the url
    CFRelease((__bridge CFTypeRef)(PDFURL));


    if (pdfDocument) {

        //get page count
        int pageCount = CGPDFDocumentGetNumberOfPages (pdfDocument);

        //
        if (pageCount > 0) {

            //set up operator table
            CGPDFOperatorTableRef pdfOpTable;
            pdfOpTable = CGPDFOperatorTableCreate();

            //call backs for Op Table
            CGPDFOperatorTableSetCallback (pdfOpTable, "MP", &op_MP);
            CGPDFOperatorTableSetCallback (pdfOpTable, "DP", &op_DP);
            CGPDFOperatorTableSetCallback (pdfOpTable, "BMC", &op_BMC);
            CGPDFOperatorTableSetCallback (pdfOpTable, "BDC", &op_BDC);
            CGPDFOperatorTableSetCallback (pdfOpTable, "EMC", &op_EMC);

            for(int i=0; i<pageCount; i++) {

                //set up
                CGPDFPageRef thisPage;
                CGPDFScannerRef pdfScanner;
                CGPDFContentStreamRef thisContentStream;

                //get the page
                thisPage = CGPDFDocumentGetPage (pdfDocument, i + 1 );

                //get the page content stream
                thisContentStream = CGPDFContentStreamCreateWithPage (thisPage);

                //create a pdf scanner using our previously created table and callbacks
                pdfScanner = CGPDFScannerCreate (thisContentStream, pdfOpTable, NULL);

                //scan the pdf file
                CGPDFScannerScan (pdfScanner); //-->call backs happen here

                //release everything
                CGPDFPageRelease (thisPage);
                /*CGPDFScannerRelease (pdfScanner);
                CGPDFContentStreamRelease (thisContentStream);*/


            }
        }
    }

    //release the pdf document
    CGPDFDocumentRelease(pdfDocument);


}

static void op_MP (CGPDFScannerRef s, void *info) {

    const char *name;

    if (!CGPDFScannerPopName(s, &name))
        return;

    NSLog(@"MP /%s\n", name);
}

static void op_DP (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    NSLog(@"DP /%s\n", name);
}

static void op_BMC (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    NSLog(@"BMC /%s\n", name);
}

static void op_BDC (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    NSLog(@"BDC /%s\n", name);
}

static void op_EMC (CGPDFScannerRef s, void *info) {
    const char *name;
    if (!CGPDFScannerPopName(s, &name))
        return;
    NSLog(@"EMC /%s\n", name);
}

编辑:PDFURL来自此过程:用户选择使用应用程序打开附加到电子邮件的PDF文件。在AppDelegate.m中:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
   /* NSLog(@"Open URL:\t%@\n"
          "From source:\t%@\n"
          "With annotation:%@",
          url, sourceApplication, annotation);*/

    NSMutableDictionary* userData = [[NSMutableDictionary alloc] init];

    [userData setObject:@"Read PDF" forKey:@"Action"];
    [userData setObject:url forKey:@"File Path"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"theMessenger" object:self userInfo: userData];

    //NSString *filepath = [url path];
    //...
    return YES;
}
在viewcontroller.m中

- (void) receiveNotification:(NSNotification* ) notification {
...
} else if ([strAction isEqualToString:@"Read PDF"]) {

            NSURL* documentURL = [[notification userInfo] objectForKey:@"File Path"];
            [self readPDFFile:documentURL];

        }
...
编辑:如果我不发布此页面应用程序仍然崩溃:

libobjc.A.dylib`objc_release: 0x3be89f20:cmp r0,#0 0x3be89f22:它是eq 0x3be89f24:bxeq lr 0x3be89f26:ldr r1,[r0] 0x3be89f28:movs r2,#2 0x3be89f2a:ldr r1,[r1,#16] 0x3be89f2c:bfi r1,r2,#0,#2 0x3be89f30:ldrb r1,[r1] 0x3be89f32:tst.w r1,#2 0x3be89f36:bne 0x3be89f3e; objc_release + 30 0x3be89f38:movs r1,#0 0x3be89f3a:b.w 0x3be981c0; - [NSObject发布] 0x3be89f3e:movw r1,#19326 0x3be89f42:movt r1,#507 0x3be89f46:添加r1,pc 0x3be89f48:ldr r1,[r1] 0x3be89f4a:b.w 0x3be875a0; objc_msgSend 0x3be89f4e:nop

1 个答案:

答案 0 :(得分:1)

不要发布这个页面..你通过GET调用得到它而不是副本或创建方法意味着你不拥有它

-

不要释放PDURL ..你从外面得到它但你不拥有它

-

确实释放了operatorTable ..你创建它

不要将它重复用于多个扫描仪