前言web
前面一段时间,一直在学习一些经常使用技术的底层的原理,由于我以为只是会用不会很好的理解也不能很好的在开发的过程当中定位出现的bug,因此花了点时间去学习category、block以及runloop(AFNetworking中的常驻线程)的一些知识,去查阅资料而后理解它们的特性(好比category只能增长方法不能增长实例变量等等),主要是想在使用的过程当中能更好的选择实现方式去解决问题,因此近段时间一直没有写博客;今天周末,新的app版本也已提交appstore,项目中有一些须要改进的地方,先提早进行相关知识点的学习,以备不时之需,好了废话很少说,开始咱们今天的内容--在IOS中预览pdf文件,显示pdf文件通常使用两种方式,一种是UIWebView,这种方式怎么说呢优势就是除了简单仍是简单,直接显示pdf文件;另外的一种是自定义UIView,配合CGPDFDocumentRef读取pdf文件里面的内容,在自定义的drawRect方法中描绘获取的pdf内容;其实还有一种的方式,就是苹果在IOS4.0后,apple推出新的文件预览控件:QLPreveiewController,支持pdf文件阅读。今天我主要写的是自定义View+CGPDFDocumentRef显示pdf文件;网络
(一)先运用CGPDFDocumentRef获取指定的pdf内容;下面是我写出的获取内容的方法;app
//用于本地pdf文件 - (CGPDFDocumentRef)pdfRefByFilePath:(NSString *)aFilePath { CFStringRef path; CFURLRef url; CGPDFDocumentRef document; path = CFStringCreateWithCString(NULL, [aFilePath UTF8String], kCFStringEncodingUTF8); url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, NO); document = CGPDFDocumentCreateWithURL(url); CFRelease(path); CFRelease(url); return document; } - (NSString *)getPdfPathByFile:(NSString *)fileName { return [[NSBundle mainBundle] pathForResource:fileName ofType:@".pdf"]; } //用于网络pdf文件 - (CGPDFDocumentRef)pdfRefByDataByUrl:(NSString *)aFileUrl { NSData *pdfData = [NSData dataWithContentsOfFile:aFileUrl]; CFDataRef dataRef = (__bridge_retained CFDataRef)(pdfData); CGDataProviderRef proRef = CGDataProviderCreateWithCFData(dataRef); CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithProvider(proRef); CGDataProviderRelease(proRef); CFRelease(dataRef); return pdfRef; }
经过文件路径解析pdf文件,返回文件内容。ide
(二)第二步就是在自定义的View中绘制pdf内容;oop
- (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdfRef, catPage);//获取指定页的内容如catPage=1,获取的是pdf第一页的内容 CGRect mediaRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);//pdf内容的rect CGContextRetain(context); CGContextSaveGState(context); [[UIColor whiteColor] set]; CGContextFillRect(context, rect);//填充背景色,不然为全黑色; CGContextTranslateCTM(context, 0, rect.size.height);//设置位移,x,y; CGFloat under_bar_height = _hasNavBar ? -64.0f : 0.0f; CGContextScaleCTM(context, rect.size.width / mediaRect.size.width, -(rect.size.height + under_bar_height) / mediaRect.size.height);//缩放倍数--x轴和y轴 CGContextSetInterpolationQuality(context, kCGInterpolationHigh); CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); CGContextDrawPDFPage(context, pageRef);//绘制pdf CGContextRestoreGState(context); CGContextRelease(context); }
以上就是绘制pdf的内容的代码,代码每行我都写了注释,其实都是很好了解的。学习
固然上面的只是显示pdf指定页的逻辑,要想显示整个pdf文件的内容,须要配合UIScrollView或者是UIPageViewController去实现,原理也很简单,结合上述的逻辑只要传入一个pageIndex的值,刷新页面内容就能够实现。下面是我写的一个列子,pageviewcontroller实现;ui
self.pdfArr = [NSMutableArray array]; NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey: UIPageViewControllerOptionSpineLocationKey]; self.pageVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options]; _pageVC.view.frame = self.view.bounds; _pageVC.delegate = self; _pageVC.dataSource = self; [self addChildViewController:_pageVC]; PDFView *testPdf = [[PDFView alloc] init]; NSString *pathStr = [[NSBundle mainBundle] pathForResource:@"test-pdf" ofType:@".pdf"]; CGPDFDocumentRef pdfRef = [testPdf createPDFFromExistFile:pathStr]; size_t count = CGPDFDocumentGetNumberOfPages(pdfRef);//这个位置主要是获取pdf页码数; for (int i = 5; i < count; i++) { PDFContentVC *pdfVC = [[PDFContentVC alloc] init]; PDFView *pdfView = [[PDFView alloc] initWithFrame:self.view.frame atPage:(i+1) andFileName:@"test-pdf"]; pdfVC.pdfView = pdfView; [pdfVC.view addSubview:pdfVC.pdfView]; [_pdfArr addObject:pdfVC]; } [_pageVC setViewControllers:[NSArray arrayWithObject:_pdfArr[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil]; [self.view addSubview:_pageVC.view]; //委托方法; - (PDFContentVC *)viewControllerAtIndex:(NSInteger)index { //Create a new view controller and pass suitable data. if (([_pdfArr count] == 0 )|| (index > [_pdfArr count]) ) { return nil; } NSLog(@"index = %ld",(long)index); return (PDFContentVC *)_pdfArr[index]; } - (NSUInteger) indexOfViewController:(PDFContentVC *)viewController { return [self.pdfArr indexOfObject:viewController]; } - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { NSUInteger index = [self indexOfViewController:(PDFContentVC *)viewController]; if (index == NSNotFound) { return nil; } index++; if (index == [_pdfArr count]){ return nil; } return [self viewControllerAtIndex:index]; } - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { NSUInteger index = [self indexOfViewController:(PDFContentVC *)viewController]; if ((index == 0 ) || (index == NSNotFound)){ return nil; } index--; return [self viewControllerAtIndex:index]; }
以上就是简单的实现;url
(三)对于第三种方法,使用QLPreveiewController去预览pdf,我就没有具体的经过代码去实现pdf的预览,在网络上应该有资源可查;线程
QLPreveiewController的用法;已经简单的说了一下怎么去预览pdf内容,也是很简单的,若是想要自定义显示的话,须要本身花时间去研究了。code
总结
无论是选用哪种方式,均可以实现加载pdf文件;webview除了简单粗暴可是只能做为简单的浏览,CGPDFDocumentRef须要开发者本身去实现显示的逻辑,但这样可控性会比较好,显示的效果也是能够自定义,QLPreveiewController的话,我没有本身去实现过,因此你们能够去试试。