我搜索了很多,但无法从文档文件夹中打开PDF。
NSString *filePath = @"/Users/***/Library/Application Support/iPhone Simulator/5.0/Applications/F2B7E9DE-9996-4F05-BC81-2A2889B4F504/Documents/Number1.pdf";
ReaderDocument *document = [ReaderDocument withDocumentFilePath:filePath password:password];
if (document != nil)
{// document comes nil here
ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
readerViewController.delegate = self; // Set the ReaderViewController delegate to self
[self.navigationController pushViewController:readerViewController animated:YES];
}
我确定filepath就是pdf文件。
在reader的示例代码中,它从主包中打开pdf。但是我需要从资源文件夹中打开。
谢谢
我也面临着同样的问题,也许你也有同样的问题。
如果您不使用ARC,只需将-fobjc-arc写入构建界面中的每个pdf阅读器文件。你的问题就解决了。
您应该使用[[NSBundle mainBundle] bundlePath]
和stringByAppendingPathComponent:
而不是硬编码字符串。这是非常可怕的,只能在iOS模拟器上工作。
你应该像下面的代码一样给出文件名,不要直接给出
NSArray *pathss = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPaths = [pathss objectAtIndex:0];
NSString *filePaths = [documentsPaths stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
我知道这是一个旧的帖子,但我遇到了一个类似的问题@user1392687,并想分享我是如何解决这个问题的(我是从不同的目录加载文件,而不仅仅是文档文件夹)。
问题:从目录中加载一系列PDF文件,用文件名和支持的元数据填充表视图,然后在选择单元格后,使用VFR Reader打开PDF文件。
解决方案:X-Code中的文件夹是一个文件夹引用,可以使内容更新,而不必执行组引用的删除/添加周期。下面的函数用于读取特定文件夹路径的所有内容- url,然后删除返回的文件路径中包含的所有/任何simlinks。在将URL传递给VRF以加载PDF文件之前,[URL path]用于rfc1808(未转义)路径。
+ (NSArray *)enumerateContentsOfFolderWithPath:(NSURL *)aFolderPath
{
NSError *error = nil;
NSArray *contentProperties = @[NSURLIsDirectoryKey,
NSURLIsReadableKey,
NSURLCreationDateKey,
NSURLContentAccessDateKey,
NSURLContentModificationDateKey];
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:aFolderPath
includingPropertiesForKeys:contentProperties
options:NSDirectoryEnumerationSkipsHiddenFiles
error:&error];
if (error != nil)
DLog(@"Content enumeration error: %@", error);
NSMutableArray *pdfURLs = [NSMutableArray array];
for (NSURL *item in contents)
{
NSURL *fileURL = [NSURL fileURLWithPath: [item path]];
NSURL *noSimlink = [fileURL URLByResolvingSymlinksInPath];
[pdfURLs addObject: noSimlink];
}
return pdfURLs;
}
在用文件夹的内容和所有支持的元数据填充表视图之后,当用户触摸一行以查看PDF文件时,VRF Reader按如下方式设置:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Other setup code...
NSURL *item = [pdfURLs objectAtIndex:(NSUInteger) indexPath.row];
[self presentPdfViewerForItem: item];
}
- (void)presentPdfViewerForItem:(NSURL *)aItem
{
NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
NSString *filePath = [aItem path];
ReaderDocument *document = [ReaderDocument withDocumentFilePath: filePath password:phrase];
if (document != nil) // Must have a valid ReaderDocument object in order to proceed
{
ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
readerViewController.delegate = self;
readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:readerViewController animated:YES completion:nil];
}
}