2

我是 IOS 开发新手,我正在开发 pdf 应用程序,我需要将 PDF 文件存储在 NSData 变量中,我有 PDF 路径,但是当我尝试将此 pdf 放在 NSData 上时出现此消息错误变量使用dataWithContentsOfFile她是我的简单代码:

NSError *error;
NSString *PdfPath = [NSString stringWithFormat:(@"%@"),document.fileURL ];
NSString *newPath = [PdfPath stringByReplacingOccurrencesOfString:@"file://localhost" withString:@""];
NSLog(@"OriginalPdfPath => %@", newPath);
NSData *pdfData = [NSData dataWithContentsOfFile:newPath options:NSDataReadingUncached error:&error];

注意:pdf 路径采用以下格式: /Users/bluesettle/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/BBEF320E-7E2A-49DA-9FCF-9CFB01CC0402/ContractApp.app/Pro.iOS.Table.Views .pdf

感谢您的帮助

4

2 回答 2

8

Cocoa 错误 260 是一个NSFileReadNoSuchFileError(如 中所列FoundationErrors.h),表示在您指定的路径中找不到该文件。

问题是您的路径仍然包含编码空格 ( %20),因为您是基于 URL 的。你可以简单地这样做:

NSData *pdfData = [NSData dataWithContentsOfFile:[document.fileURL path]];
于 2013-05-06T08:57:45.690 回答
1

尝试使用NSBundle

NSString *newPath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"pdf"]

编辑:

比你可以使用bundleWithPath的方法,这里是一个例子:

NSString *documentsDir= [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()];

NSString *newPath= [[NSBundle bundleWithPath:documentsDir] bundlePath];
于 2013-05-06T08:35:40.897 回答