There are as you may already know 2 ways of rendering PDF's:
- UIWebView
- Quartz Rendering
The Other answers at the time of writing this have focused on Quartz. There are a number of good reasons for this mostly performance related but in my opinion using Quartz is worth it. I would recommend reading this thread to get a better idea of the pro's and cons.
There is apparently an excellent newish api for Quartz based pdf rendering here
ofc you could present the pdf via UIWebView and render the thumbs using quartz.
There is also a bit of confussion around thumbs, for people new quartz pdf magic, it might seem that after some searching there are apis that support thumbs abut you should check if the support is for embedded thumbs only, many PDF's don't have these.
Another option is to create the thumbs yourself (using quartz) and there are plenty of examples of this around the net including the two answers above. However If you are targeting iOS 4 or above I would strongly recommend using blocks. (Also graphics contexts are thread safe since 4).
I found a significant performance increase when I generated thumbs with blocks.
What I have done in the past is:
Have a ViewController for your
thumbs, it has a scrollview that has
a content size appropriate for all
your pages. Insert Placeholder
ImageViews into is if you like.
On document load, kick off a thumb
generator in the background (see code
below)
The code below calls a method drawImageView
that takes the index of the page, grabs the image from the disk and puts it into the scroll view
If your feeling really motivated you can implement a render scope on the thumb scrollView (only rendering the thumbs you need to - something you should be doing for the pdf's anyway)
Dont forget to delete the thumbs when your done, unless you 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);
}
- (UIImage *)imageFromPDFWithDocumentRef:(CGPDFDocumentRef)documentRef and PageNumber:(int)pageNumber { CGPDFPageRef pageRef = CGPDFDocumentGetPage(documentRef, pageNumber);
then call it in a loop. – Luke Mcneice Apr 15 '11 at 08:48