Using QLPreviewController to display files from Documents folder
I very much appreciate the tutorial on using QLPreviewController
here.
I am trying to implement this code into my application, with modification to display files from the app's Documents folder instead of the Resources folder (so that the user can use iTunes filesharing to manage the documents). However, in doing so I hit the "EXC_BAD_ACCESS
" error.
I create an array of files in the Documents folder to produce the tableview list:
- (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];
}
I have no problem with displaying a list of files from the Documents folder in this way.
The code to display a chosen file from the list using QuickLook is:
- (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 objectA开发者_如何学运维tIndex:0] ofType:[fileComponents objectAtIndex:1] inDirectory: @"Documents"];
return [NSURL fileURLWithPath:path];
}
When running this code, I receive the error EXC_BAD_ACCESS
at the line:
[previewer setDataSource:self];
Any help would be much-appreciated. Thanks in advance.
精彩评论