2010-12-12 101 views
4

我正在創建一個需要訪問文檔目錄的應用程序。我目前正在使用以下內容從主包中返回文件pdfName的URL。是否有類似的方式獲取文檔目錄?CFURL參考文檔目錄

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)pdfName, NULL, NULL); 

編輯:這是我完整的代碼,但它不工作 - 任何想法?

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *myFilePath = [documentsDirectory stringByAppendingPathComponent:pdfName]; 

    CFURLRef pdfURL = (CFURLRef)[NSURL fileURLWithPath:myFilePath]; 
    pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); 
    CFRelease(pdfURL); 

回答

10

我有同樣的問題。該應用程序在創建CFURLRef時崩潰。這是我如何解決它(因爲你已經在文件目錄到文件的完整路徑一個NSString):

CFURLRef pdfURL = (__bridge CFURLRef)[[NSURL alloc] initFileURLWithPath:myFilePath]; 
pdf = CGPDFDocumentCreateWithURL(pdfURL); 
CFRelease(pdfURL); 

貌似在我的代碼,唯一的區別是,我alloc和初始化的NSURL。

0

這可以幫助你:ADC Link

+0

我看了一下,並取得了一些進展(見上面的更新)。關於我當前的代碼有什麼問題的任何想法? – Jack 2010-12-12 14:25:22

+0

NSSearchPathForDirectoriesInDomains返回一個NSArray看到http://bit.ly/g7pNrF,我在網上找到一個例子http://bit.ly/fSbJLY :)希望它有幫助。 – lm2s 2010-12-12 17:23:13