2011-05-24 2 views
7

J'affiche un PDF dans un UIScrollView. Pour ce faire, je l'utilise:Comment convertir un chemin NSString en CFURLRef (pour iOS 3.1)?

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

Maintenant, j'essaie de le faire fonctionner sur un IOS 3.1 (NSBundle URLForResource: withExtension n'existe pas pour 3.1)

Je tournais mon code dans:

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

Mais elle conduit à une

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

Je précise que NSLog enregistre

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

Comment résoudre ce problème?

Répondre

9

NSURL est interchangeable avec CFURLRef grâce au pont sans frais. Essayez ceci:

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

Il est important de mentionner que cela ne fonctionnera plus si vous utilisez ARC. Par conséquent, vous devrez créer un casting ponté. Voir ci-dessous:

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