繁体   English   中英

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

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

我在 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"

我该如何解决?

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

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

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

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