使用QLPreviewController显示Documents文件夹中的文件

use*_*804 5 iphone qlpreviewcontroller

我非常感谢这里使用的教程QLPreviewController .

我正在尝试将此代码实现到我的应用程序中,并修改了从应用程序的Documents文件夹而不是Resources文件夹中显示文件(以便用户可以使用iTunes文件共享来管理文档).但是,这样做我遇到了" EXC_BAD_ACCESS"错误.

我在Documents文件夹中创建一个文件数组来生成tableview列表:

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

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

NSString *documentsPath = [paths objectAtIndex:0];

arrayOfDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:NULL];
}
Run Code Online (Sandbox Code Playgroud)

以这种方式显示Documents文件夹中的文件列表没有问题.

使用QuickLook从列表中显示所选文件的代码是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
// When user taps a row, create the preview controller
QLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];

// Set data source
[previewer setDataSource:self];

    // Which item to preview
[previewer setCurrentPreviewItemIndex:indexPath.row];

// Push new viewcontroller, previewing the document
[[self navigationController] pushViewController:previewer animated:YES];
}


- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller 
{
return [arrayOfDocuments count];
}


- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index 
{
// Break the path into it's components (filename and extension)
NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];

// Use the filename (index 0) and the extension (index 1) to get path
NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1] inDirectory: @"Documents"];

return [NSURL fileURLWithPath:path];
}
Run Code Online (Sandbox Code Playgroud)

运行此代码时,我收到EXC_BAD_ACCESS该行的错误:

[previewer setDataSource:self];
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激.提前致谢.