简体   繁体   English

如何将 NSString 路径转换为 CFURLRef(对于 iOS 3.1)?

[英]How to convert NSString path to CFURLRef (for iOS 3.1)?

I display a PDF in a UIScrollView.我在 UIScrollView 中显示 PDF。 To do so I use:为此,我使用:

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

Now, I try to make it work on an IOS 3.1 (NSBundle URLForResource:withExtension does not exist for 3.1)现在,我尝试让它在 IOS 3.1 上工作(NSBundle URLForResource:withExtension 不存在于 3.1)

I turned my code into:我把我的代码变成了:

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);

But it leads to a但这会导致

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

I precise that NSLog logs我准确地说 NSLog 日志

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

How do I fix this?我该如何解决?

NSURL is interchangeable with CFURLRef thanks to toll-free bridging.由于免费桥接,NSURL 可以与 CFURLRef 互换 Try this:尝试这个:

NSString *pdfPath = [[NSBundle mainBundle] 
                     pathForResource:@"salonMap" ofType:@"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pdfPath];
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);

It's important to mention that this will no longer work if you are using ARC.重要的是要提到,如果您使用 ARC,这将不再有效。 As a result you will need to create a bridged cast.因此,您将需要创建一个桥接演员表。 See below:见下文:

NSString *pdfPath = [[NSBundle mainBundle] 
                 pathForResource:@"salonMap" ofType:@"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pdfPath];
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM