您可能已经知道 2 种渲染 PDF 的方法:
在撰写本文时,其他答案都集中在 Quartz 上。这主要与性能相关有很多很好的理由,但我认为使用 Quartz 是值得的。我建议阅读 this thread 以更好地了解优缺点。
显然有一个出色的基于 Quartz 的 pdf 渲染的新 API here
您可以通过 UIWebView 呈现 pdf 并使用石英渲染拇指。
还有一些关于拇指的困惑,对于人们新的石英 pdf 魔法,经过一番搜索后似乎有支持拇指的 API,您应该检查是否支持 嵌入式仅限拇指,许多 PDF 没有这些。
另一种选择是自己创建拇指(使用石英),网上有很多这样的例子,包括上面的两个答案。但是,如果您的目标是 iOS 4 或更高版本,我强烈建议您使用块。 (从 4 开始,图形上下文也是线程安全的)。
我发现使用块生成拇指时性能显着提高。
我过去做过的是:
为你的 ViewController
大拇指,它有一个滚动视图
适合所有人的内容大小
你的页面。插入占位符
如果您愿意,可以将 ImageViews 放入。
在加载文档时,启动拇指
后台生成器(见代码
下面)
下面的代码调用了一个方法drawImageView,它获取页面的索引,从磁盘中抓取图像并将其放入滚动视图中
如果你真的有动力,你可以在拇指滚动视图上实现一个渲染范围(只渲染你需要的拇指 - 无论如何你应该为 pdf 做一些事情)
完成后不要忘记删除拇指,除非你想缓存..
#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);
}