簡體   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