As you already know, there are two ways to render PDF:
- UIWebView
- Quartz rendering
Other answers at the time of this writing have focused on Quartz. There are several good reasons for this, mainly related to work, but in my opinion, the use of quartz is worth it. I would recommend reading this topic to better understand the pros and cons.
There seems to be an excellent new qui-based PDF rendering api here
ofc you can submit a PDF via UIWebView and make thumbs with quartz.
There are also a few misunderstandings regarding the thumbs, for people creating a new quartz PDF mockup, it might seem that after some searches there are apis that support the thumbs, you should check if support supports the built-in preview only, many PDFs do not have these .
Another option is to create thumbs (using quartz), and there are many examples of this on the net, including the two answers above. However, if you are targeting iOS 4 or higher, I highly recommend using blocks. (Also graphical contexts are thread safe with 4).
I found a significant increase in performance when I generated thumbs with blocks.
What I have done in the past is:
You have a ViewController for your preview, it has a scrollview that has a content size that fits all of your pages. Embed ImageViews placeholder in if you like.
When loading a document, release the thumb generator in the background (see code below)
In the code below, the drawImageView
method is drawImageView
, which takes the index of the page, grabs the image from disk, and puts it in the scroll view
If your feeling is really motivated, you can implement the visualization area on the scrollView of your thumb (just to make the thumbs you need - something you should do for pdf anyway)
Remember to remove the thumbs at runtime if you do not want to cache.
#define THUMB_SIZE 100,144
-(void)generateThumbsWithGCD { thumbQueue = dispatch_queue_create("thumbQueue", 0);//thumbQueue = dispatch_queue_t NSFileManager *fm = [NSFileManager defaultManager]; //good idea to check for previous thumb cache with NSFileManager here CGSize thumbSize = CGSizeMake(THUMB_SIZE); __block CGPDFPageRef myPageRef; NSString *reqSysVer = @"4.0"; NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; //need to handle ios versions < 4 if ([currSysVer compare:reqSysVer options:NSNumericSearch] == NSOrderedAscending) {NSLog(@"UIKIT MULTITHREADING NOT SUPPORTED!");return;}//thread/api saftey dispatch_async(thumbQueue, ^{ for (i=1; i<=_maxPages; i++) { //check if worker is valid (class member bool) for cancelations myPageRef=[[PDFDocument sharedPDFDocument]getPageData:i];//pdfdocument is a singleton class if(!myPageRef)return; NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString* imageName = [NSString stringWithFormat:@"%@thumb%i.png",documentName,i]; NSString* fullPathToFile = [thumbDocPath stringByAppendingPathComponent:imageName]; if(![fm fileExistsAtPath:fullPathToFile]){ //NSLog(@"Not there"); UIGraphicsBeginImageContext(thumbSize);//thread Safe in iOs4 CGContextRef context = UIGraphicsGetCurrentContext();//thread Safe in iOs4 CGContextTranslateCTM(context, 0, 144); CGContextScaleCTM(context, 0.15, -0.15); CGContextDrawPDFPage (context, myPageRef); UIImage * render = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSData* imageData= UIImagePNGRepresentation(render); if(imageData){ NSLog(@"WROTE TO:%@",fullPathToFile); if(![imageData writeToFile:fullPathToFile atomically:NO])NSLog(@"ERROR: Thumb Didnt Save"); //COMMENT OUT TO DISABLE WRITE } } else NSLog(@"Allready There! %@",fullPathToFile); //update progress on thumb viewController if you wish here [pool release]; dispatch_sync(dispatch_get_main_queue(), ^{ [self drawImageView:i]; }); } }); dispatch_release(thumbQueue); }
code>