iOS PDF Reader

For a new project we're working on (a book app for children, based on Rupert the Explorer), we were looking for a PDF reading module for iPad.  After investigating the possibility to write a module ourself, we checked on Github for possible modules, and found 2 possible candidates: iOS-PDF-Reader and Reader.  We tried iOS-PDF-Reader first, as it was less bloated with overlays and allowed to easily create UIImage's from PDF pages (which was required to generate a screenshot of the front pages of the books dynamically).  After using it on an iPad 1 on a real life sample, it was just too slow to be usefull (it does not seem to rely on background processing to create lowres versions, ...).

 Rupert PDF Reader screenshot

So we switched to Reader as PDF reader, and kept iOS-PDF-Reader for generating the cover screenshots, and the result is great!  Below is the code we use to generate the screenshots (in a background thread, for performance reasons and cache them use EGOCache) as well as the code to start the PDF reader given the URL of the PDF document.  Plain easy, and a super result!

Screenshot generation

[objc] UIImage* img = [[EGOCache currentCache] imageForKey:[url lastPathComponent]]; if (!img) { dispatch_async(dispatch_get_global_queue(0, 0), ^{ CPDFDocument* doc = [[CPDFDocument alloc] initWithURL:url]; CPDFPage* page = [[CPDFPage alloc] initWithDocument:doc pageNumber:1]; UIImage* img = [page imageWithSize:CGSizeMake(173, 128)]; [[EGOCache currentCache] setImage:img forKey:[url lastPathComponent]]; dispatch_async(dispatch_get_main_queue(), ^{ [btn setImage:img forState:UIControlStateNormal]; }); }); } else [btn setImage:img forState:UIControlStateNormal]; [/objc]

Launch PDF reader

[objc] ReaderDocument* doc = [[ReaderDocument alloc] initWithFilePath:[url path] password:nil]; ReaderViewController* ctrl = [[ReaderViewController alloc] initWithReaderDocument:doc]; ctrl.delegate = self; ctrl.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentViewController:ctrl animated:YES completion:nil]; [/objc]

Easy, not?



PS: For those of you wondering about memory management: since this project is iOS5 only, I rely on ARC to do my app's memory management.  And I love it!  More on ARC and Storyboarding (which is also used in this project) in a later post.