7

我在 UIScrollView 中显示 PDF。为此,我使用:

myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)[[NSBundle mainBundle] URLForResource:@"salonMap" withExtension:@"pdf"]);

现在,我尝试让它在 IOS 3.1 上工作(NSBundle URLForResource:withExtension 不存在于 3.1)

我把我的代码变成了:

NSString *fullPath =  [[NSBundle mainBundle] pathForResource:@"salonMap" ofType:@"pdf"];
NSLog(@"%@", fullPath);
CFStringRef fullPathEscaped = CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)fullPath, NULL, NULL,kCFStringEncodingUTF8);
CFURLRef urlRef = CFURLCreateWithString(NULL, fullPathEscaped, NULL);
myDocumentRef = CGPDFDocumentCreateWithURL(urlRef);

但这会导致

<Error>: CFURLCreateDataAndPropertiesFromResource: failed with error code -15

我准确地说 NSLog 日志

/var/mobile/Applications/1CF69390-85C7-45DA-8981-A279464E3249/myapp.app/salonMap.pdf"

我该如何解决?

4

2 回答 2

9

由于免费桥接,NSURL可以与 CFURLRef互换。试试这个:

NSString *pdfPath = [[NSBundle mainBundle] 
                     pathForResource:@"salonMap" ofType:@"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pdfPath];
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
于 2011-05-24T17:09:33.653 回答
2

重要的是要提到,如果您使用 ARC,这将不再有效。因此,您将需要创建一个桥接演员表。见下文:

NSString *pdfPath = [[NSBundle mainBundle] 
                 pathForResource:@"salonMap" ofType:@"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pdfPath];
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
于 2013-07-01T14:10:51.350 回答