Iphone I';我无法缓存现有文件的路径
这是我的代码,我使用Iphone I';我无法缓存现有文件的路径,iphone,ios,ipad,Iphone,Ios,Ipad,这是我的代码,我使用qlviewcontroller来显示一个带有pdf文件的详细视图,我在程序开始时下载并储存了该文件 它抛出一个异常,告诉路径是null -(id<QLPreviewItem>)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)index{ NSString *name=[selectedItem.source last
qlviewcontroller
来显示一个带有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!!
}
-(id)previewController:(QLPreviewController*)previewController PreviewWitematindex:(NSInteger)索引{
NSString*name=[selectedItem.source lastPathComponent];
NSFileManager*fileManager=[NSFileManager defaultManager];
如果([fileManager fileExistsAtPath:name]){
NSLog(@“%@exsit!”,name);//在!!!
}
名称=[name stringByDeletingPathExtension];
NSLog(@“name for QL:%@”,name);//name ok!!!
NSString*path=[[NSBundle mainBundle]pathForResource:类型名称:@“pdf”];
NSLog(@“path:%@”,path);//path:null!!!
返回[NSURL fileURLWithPath:path];//错误!!
}
文件不下载到[NSBundle mainBundle]
。试着把那行改成
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathString = [documentsDirectory stringByAppendingPathComponent:fileName];`
而不是从[NSBundle mainBundle]
读取。您无法将文件下载到mainBundle。它是只读的