1

我的应用程序包中有文件test.pdf。我在 iPhone Simulator (iPad) 中运行测试,但无法填充 CGPDFDocumentRef。我是疯了还是怎么了?!?

这是专门为此(ViewController 类)制作的示例应用程序中的一段代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // below code to show files in app bundle...
 NSString *path = [[NSBundle mainBundle] resourcePath];
 NSFileManager *fm = [NSFileManager defaultManager];
 NSError *error = [[NSError alloc] init];
 NSArray *directoryAndFileNames = [fm contentsOfDirectoryAtPath:path error:&error];

 //just to make sure the file is there : (test.pdf)
 for (NSString *ts in directoryAndFileNames) {
  NSLog(ts);  // NSLog confirms the file is there
 }

 //Function below is supposed to create the CDPDFDocumentRef
 [self updatePageRef]; // try to get the CGPDFDocumentRef
}

-(BOOL)updatePageRef {
 NSLog(@"Updating PAGE");

 NSString *pdfStringURL = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
 NSURL *pdfURL = [NSURL fileURLWithPath:pdfStringURL];
 NSLog([pdfURL path]); // this is good

 NSLog(@"File Exists = %d", [[NSFileManager defaultManager] fileExistsAtPath:pdfStringURL]); // this gives me 1 (YES)

 pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);    
    //from here on the pdf is nil??????????!!!!!!!!!!!!!

 if (pdf && CGPDFDocumentGetNumberOfPages(pdf) > 0) {
  return YES;
 }

 return NO;
}

以上pdf是在@interface 中声明的,因为CGPDFDocumentRef pdf; 我知道PDF 文件很好,因为我已在“预览”中成功打开它。

一切都编译并运行,没有任何警告或错误。

帮助!

4

1 回答 1

1

我找到了解决方案,很抱歉首先问。尽管 PDF 在“预览”中完美显示,但它在某种程度上并不好。我放了另一个 pdf,将其命名为“test.pdf”,一切顺利。

干杯!

于 2010-08-28T00:05:30.157 回答