我在缓存现有文件的路径时遇到问题

时间:2011-07-12 14:21:22

标签: iphone ios ipad

这是我的代码,我使用QLPreviewController来显示详细视图,其中包含我在程序开头下载并存储的pdf文件。

它告诉路径是null

抛出异常
 -(id<QLPreviewItem>)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index{
        NSString *name=[selectedItem.source lastPathComponent];
        NSFileManager *fileManager=[NSFileManager defaultManager];
        if([fileManager fileExistsAtPath:name] ){
            NSLog(@"%@ exsit! ",name);  //In!!! 
        }


        name = [name stringByDeletingPathExtension];
           NSLog(@"name for QL: %@",name);  //Name ok!!!
        NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:@"pdf"];
         NSLog(@"path: %@",path);   //Path: null !!!




        return [NSURL fileURLWithPath:path]; //error!!
    }

1 个答案:

答案 0 :(得分:0)

文件无法下载到[NSBundle mainBundle]。尝试将该行更改为

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathString = [documentsDirectory stringByAppendingPathComponent:fileName];` 

而不是从[NSBundle mainBundle]读取。您无法将文件下载到mainBundle。这是只读的。