2011-05-24 10 views
7

Muestro un PDF en UIScrollView. Para ello utilizo:Cómo convertir la ruta de NSString a CFURLRef (para iOS 3.1)?

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

Ahora, trato de hacer que funcione en un IOS 3.1 (NSBundle URLForResource: withExtension no existe para 3.1)

Volví código en:

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

pero conduce a un

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

que precisa que NSLog registra

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

¿Cómo puedo solucionar esto?

Respuesta

9

NSURL es interchangeable con CFURLRef gracias al puente sin cargo. Pruebe esto:

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

Es importante mencionar que esto ya no funcionará si está utilizando ARC. Como resultado, necesitarás crear un reparto en puente. Vea a continuación:

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