2010-10-08 87 views
0

不想從資源文件夾加載PDF,我想從 從文檔目錄加載它。我一直試圖做這個 幾天,但CGPDFDocumentRef保持返回NULL。這裏是我的 代碼:從文檔目錄加載PDF - iPhone

   // Get Documents Directory 
       NSArray *searchPaths = 
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES); 
       NSString *documentsDirectoryPath = [searchPaths objectAtIndex:0]; 
       NSString *tempPath = [documentsDirectoryPath 
stringByAppendingPathComponent:[appDelegate.issueToLoad 
stringByAppendingPathExtension:@"pdf"]]; 
       NSString *path = [tempPath 
stringByReplacingOccurrencesOfString:@"localhost/" withString:@""]; 
       NSLog(@"PATH: %@", path); 
       //Display PDF 
       CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL, 
(CFStringRef)path, kCFURLPOSIXPathStyle, FALSE); 
       NSLog(@"PDF URL: %@", pdfURL); 
       pdf = CGPDFDocumentCreateWithURL(finalURL); 
       NSLog(@"PDF: %@", pdf); 

文件路徑是正確的,我已經檢查了模擬器文件 目錄和文件肯定是有。當我運行應用程序 最後NSLog說(空),並顯示一個空白頁面PDF。 任何想法是什麼錯? 謝謝!

+0

假設pdf是CGPDFDocumentRef,我們使用類似的代碼。它在我的工作雖然。不明白爲什麼你的失敗。 – Altealice 2010-10-08 20:00:50

+1

是什麼讓你認爲「localhost /」將成爲文件路徑名的一部分,你需要從那裏刪除它?它通常只會出現在網址中,但您尚未創建網址。 – 2010-10-09 02:03:21

回答

1

好的,下次嘗試。我希望這一次它會更有用: -/

你確定文件存在於這個路徑嗎?
我創建了一個類似於你的測試用例。和一個名爲「文件123.pdf」的文件,這似乎工作。至少我可以閱讀pdf的版本。

我在您的代碼示例後添加了此代碼,以查看PDF是否已加載。

NSLog(@"PDF: %@", pdf); 
int majorVersion; 
int minorVersion; 
CGPDFDocumentGetVersion(pdf, &majorVersion, &minorVersion); 
NSLog(@"%d %d", majorVersion, minorVersion); 

,這給了我下面的控制檯輸出:

2010-10-08 13:01:40.246 test[3517:207] PATH: /var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file 123.pdf 
2010-10-08 13:01:40.252 test[3517:207] URL: file://localhost/var/mobile/Applications/E9CDCAC1-430D-488E-ABC3-33F40F6A06F4/Documents/file%20123.pdf 
2010-10-08 13:01:40.257 test[3517:207] PDF: <NSCFType: 0x139660> 
2010-10-08 13:01:40.260 test[3517:207] 1 4 

有明明是20%裏面,所以我覺得這是不是你的執行的問題。


編輯: 添加到您的代碼,以確保該文件存在於這條道路。

if (![[NSFileManager defaultManager] fileExistsAtPath:path]) 
    NSLog(@"File does not exist!"); 

必須有你的路徑有效 pdf文件。

2

顯而易見的答案是使用pdfCurl函數中的pdfURL。 finalURL從哪裏冒出來,它並不明顯是甚麼。

pdf = CGPDFDocumentCreateWithURL(pdfURL);

0

你可能沒有在這條道路的PDF文件。 CGPDFDocumentCreateWithURL中的「創建」引用CGPDFDocument對象;它不會在該URL創建文檔文件。您只能爲已存在的PDF文件創建CGPDFDocument對象。

另外,正如我在你的其他問題中提到的,我不明白你爲什麼要從路徑中刪除「localhost /」。首先它不太可能存在(它更可能只出現在URL中),並且如果它出現在那裏,它應該應該出現在那裏,並且將其除去會使路徑錯誤。