4

如果我在项目中显示我的捆绑包中的 PDF 文件系统,我的 QLPreviewController 正在工作,

像这个例子

但是如果我想显示文件系统中的 PDF,它没有显示,我该如何调用我刚刚下载到文件系统中的 pdf 文件?

编码:

- (void)initPDFviewer
{
    QLPreviewController *previewController=[[QLPreviewController alloc]init];
    previewController.delegate=self;
    previewController.dataSource=self;
    [self presentModalViewController:previewController animated:YES];
    [previewController.navigationItem setRightBarButtonItem:nil];
}

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return 1;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
    //return 0;


    //return url!


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSLog(@"paths :: %@", paths);

    return 0;

}

所以我的问题是如何获取 pdf [仅文档目录中的 pdf],我如何获取此 pdf?

谢谢

谢谢!

4

2 回答 2

6

将您的 init 方法更改为以下。

-(id)init
{
    if (self = [super init])
    {
        //  arrayOfDocuments = [[NSArray alloc] initWithObjects: 
        //          @"iOSDevTips.png", @"Remodel.xls", @"Core J2ME Technology.pdf", nil];

         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
         NSString *docPath = [paths lastObject];

         NSArray *docArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:docPath error:nil];

         arrayOfDocuments = [[NSArray alloc] initWithArray:docArray];
    }
    return self;
}

添加这个新方法

-(NSString*)documentDirectoryPathForResource:(NSString*)aFileName 
{
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
     NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:aFileName];
     return fullPath;
}

并用我的方法替换你的方法

- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index 
{
      // Break the path into it's components (filename and extension)
      NSString *path = [self documentDirectoryPathForResource:arrayOfDocuments[index]];

      return [NSURL fileURLWithPath:path];
}

代码是不言自明的。只需创建文档目录中所有文档的数组。然后使用该数组创建路径并将该路径的 URL 提供给 QLPreviewController

于 2013-08-16T05:08:52.880 回答
1
NSURL *fileURL = nil;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *wordFilePath = [documentsDirectory stringByAppendingPathComponent:@"sample.doc"];

fileURL = [NSURL fileURLWithPath:wordFilePath];

return fileURL;
于 2014-09-04T06:02:03.243 回答