0

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 !

2 Answers2

1

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.

combinatorial
  • 9,132
  • 4
  • 40
  • 58
0

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.

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • My pdf file isn't really "test.pdf", I just wrote this as an example. I correct it with the good path. – Sébastien Polytech' Oct 03 '12 at 23:50
  • You can also see with the url that the link send you to a good pdf file, so where is the problem ? – Sébastien Polytech' Oct 03 '12 at 23:52
  • The problem is that I can't have guessed that you didn't show your real code in your question and had to suppose that you used the actual path and tested the URL. You should explain that and what you tried to fix the code in your question, we can't guess it. – AliSoftware Oct 03 '12 at 23:55
  • I know and I'm sorry for it. I edit my question with the real code and a better explanation, can you help me ? – Sébastien Polytech' Oct 04 '12 at 00:01