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

khe*_*aud 7 iphone nsurl cfstring nsbundle ios

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

myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)[[NSBundle mainBundle] URLForResource:@"salonMap" withExtension:@"pdf"]);
Run Code Online (Sandbox Code Playgroud)

现在,我尝试使其适用于IOS 3.1(NSBundle URLForResource:3.1中不存在withExtension)

我将代码转换为:

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);
Run Code Online (Sandbox Code Playgroud)

但它导致了一个

<Error>: CFURLCreateDataAndPropertiesFromResource: failed with error code -15
Run Code Online (Sandbox Code Playgroud)

我确切地说NSLog记录了

/var/mobile/Applications/1CF69390-85C7-45DA-8981-A279464E3249/myapp.app/salonMap.pdf"
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

tit*_*coy 9

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

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