我非常感谢使用QLPreviewController
here.
我正在尝试将此代码实现到我的应用程序中,并修改了从应用程序的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];
}
以这种方式显示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];
}
运行此代码时,我在以下行收到错误EXC_BAD_ACCESS
:
[previewer setDataSource:self];
任何帮助都会受到高度赞赏。提前谢谢。