2011-05-24 61 views
7

我在UIScrollView中顯示PDF。要做到這一點我使用:如何將NSString路徑轉換爲CFURLRef(適用於iOS 3.1)?

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" 

我該如何解決這個問題?

回答

9

NSURL是interchangeable與CFURLRef感謝免費橋接。試試這個:

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

重要的是要提到,如果您使用ARC,這將不再有效。因此您需要創建一個橋接模型。見下:

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