前言
前面一段时间,一直在学习一些常用技术的底层的原理,因为我觉得只是会用不会很好的理解也不能很好的在开发的过程中定位出现的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内容;下面是我写出的获取内容的方法;
//用于本地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文件,返回文件内容。
(二)第二步就是在自定义的View中绘制pdf内容;
- (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实现;
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];
}
以上就是简单的实现;
(三)对于第三种方法,使用QLPreveiewController去预览pdf,我就没有具体的通过代码去实现pdf的预览,在网络上应该有资源可查;
QLPreveiewController的用法;已经简单的说了一下怎么去预览pdf内容,也是很简单的,如果想要自定义显示的话,需要自己花时间去研究了。
总结
不管是选用哪一种方式,都可以实现加载pdf文件;webview除了简单粗暴但是只能作为简单的浏览,CGPDFDocumentRef需要开发者自己去实现显示的逻辑,但这样可控性会比较好,显示的效果也是可以自定义,QLPreveiewController的话,我没有自己去实现过,所以大家可以去试试。