CGImageSourceCreateImageAtIndex returns nil

I'm trying to convert a pdf file to an image with this solution :

CFURLRef urlRef = (CFURLRef)[NSURL fileURLWithPath:@"http://uat.okitoo.fr/document/document/s595_506cb1811852f/o_specimen-page-seule.pdf"];
CGImageSourceRef src = CGImageSourceCreateWithURL(urlRef, NULL);
NSDictionary* options = [NSDictionary dictionaryWithObject:(id)[NSNumber numberWithInt:500] forKey:(id)kCGImageSourceThumbnailMaxPixelSize];
CGImageRef firstPage = CGImageSourceCreateImageAtIndex(src, 0, (CFDictionaryRef)options);
UIImage* imagePDF = [UIImage imageWithCGImage:firstPage];
CGImageRelease(firstPage);

My pdf file is ok but when I try this code, the image doesn't appear and I have this message : ": ImageIO: CGImageSourceCreateImageAtIndex image source parameter is nil". I can't understand..

I tried with other links but I have the same problem..

Any ideas ? Is there an other way to convert my pdf file to an image ?

Thanks a lot !

Upvotes: 0

Views: 3479

Answers (2)

combinatorial
combinatorial

Reputation: 9561

You are using the incorrect API to create the URL, you need URLWithString:

CFURLRef urlRef = (CFURLRef)[NSURL URLWithString:@"http://uat.okitoo.fr/document/document/s595_506cb1811852f/o_specimen-page-seule.pdf"];

The API you are currently using is for urls to files on your local file system.

Upvotes: 1

AliSoftware
AliSoftware

Reputation: 32681

As the message says, your urlRef is probably nil.

Probably because you didn't mention the real full path but only the file name, and it can't find the file.

If your PDF is a file inside your application bundle, use [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]. If you downloaded it at runtime to some place in your sandbox, simply use the path to where the PDF has been downloaded.

Upvotes: 0

Related Questions