我尝试将NSString ( documents目录中文件的路径)转换为NSURL,但NSURL始终为空。下面是我的代码:
NSURL *urlToPDF = [NSURL URLWithString:appDelegate.pdfString];
NSLog(@"AD: %@", appDelegate.pdfString);
NSLog(@"PDF: %@", urlToPDF);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)urlToPDF);下面是日志:
2012-03-20 18:31:49.074 The Record[1496:15503] AD: /Users/John/Library/Application Support/iPhone Simulator/5.1/Applications/E1F20602-0658-464D-8DDC-52A842CD8146/Documents/issues/3.1.12/March 1, 2012.pdf
2012-03-20 18:31:49.074 The Record[1496:15503] PDF: (null)我认为部分问题可能是因为NSString包含斜杠/和破折号-。我做错了什么?谢谢。
发布于 2012-03-21 06:38:36
为什么你不这样创建你的文件路径呢?
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"pdfName" ofType:@"pdf"];然后像这样用文件路径创建你的url。
NSURL *url = [NSURL fileURLWithPath:filePath];发布于 2012-03-21 06:42:16
问题是,appDelegate.pdfString不是一个有效的网址,它是一个路径。file URL看起来像这样:
file://host/path或者对于本地主机:
file:///path所以你实际上想要:
NSURL *urlToPDF = [NSURL URLWithString:[NSString stringWithFormat:@"file:///%@", appDelegate.pdfString]];...except您的路径包含空格,需要对其进行URL编码,因此您实际上希望:
NSURL *urlToPDF = [NSURL URLWithString:[NSString stringWithFormat:@"file:///%@", [appDelegate.pdfString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];https://stackoverflow.com/questions/9796023
复制相似问题