首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS:读取文件内容

iOS:读取文件内容
EN

Stack Overflow用户
提问于 2013-05-24 03:30:55
回答 1查看 639关注 0票数 0

通过查阅Quartz 2D编程指南和一些文章,我拼凑了一些代码来读取PDF文件的内容。它在PDF扫描器部分失败了,我想。当我要释放扫描仪时,应用程序崩溃,并显示以下错误:

代码语言:javascript
复制
avi(2282,0x3de7bb88) malloc: *** error for object 0x693f7c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

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

代码语言:javascript
复制
- (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中:

代码语言:javascript
复制
- (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;
}

在视图控制器中。m

代码语言:javascript
复制
- (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: it 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:无

EN

回答 1

Stack Overflow用户

发布于 2013-05-24 03:42:19

不要释放thisPage..您通过get调用而不是copy或create方法获取它,这意味着您不拥有它

--

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

--

请务必释放operatorTable ..您可以创建它

不要在多台扫描仪中重复使用它

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16722330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档