我是IOS开发方面的新手,我正在开发一个pdf应用程序,我需要在一个dataWithContentsOfFile变量上存储一个PDF文件,我有PDF路径,但是当我试图使用NSData将这个pdf放到NSData变量上时,我得到了这个消息错误--她是我的简单代码:
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
谢谢你的帮忙
发布于 2013-05-06 08:57:45
可可错误260是一个NSFileReadNoSuchFileError (如FoundationErrors.h中所列),这意味着在您指定的路径上找不到该文件。
问题是您的路径仍然包含编码空格(%20),因为它是基于URL的。您可以简单地这样做:
NSData *pdfData = [NSData dataWithContentsOfFile:[document.fileURL path]];发布于 2013-05-06 08:35:40
尝试使用NSBundle
NSString *newPath = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"pdf"]编辑:
以下是一个可以使用bundleWithPath方法的示例:
NSString *documentsDir= [NSString stringWithFormat:@"%@/Documents", NSHomeDirectory()];
NSString *newPath= [[NSBundle bundleWithPath:documentsDir] bundlePath];https://stackoverflow.com/questions/16394876
复制相似问题