之前的老项目遇到了一个问题:iOS9后PDF中文会显示乱码,而且试了各种方法都不行,还好最终找到了解决方法---MuPDF
在此总结一下解决该问题过程中使用过的方法:
//代码很简单 UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:webView]; NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]]; [webView loadRequest:request];
//初始化QLPreviewController对象 QLPreviewController *previewController = [[QLPreviewController alloc] init]; previewController.dataSource = self; [self.navigationController pushViewController:pageController animated:YES]; //再实现QLPreviewControllerDataSource的两个方法即可显示 //显示文件数量 - (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller; //文件路径URL - (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index;
在UIView
的- (void)drawRect:(CGRect)rect
方法中绘制PDF内容,代码如下:
/* Quartz2D UIKit y (0, 0)|----------x | | | | | | | | (0, 0) |---------x y */ - (void)drawRect:(CGRect)rect { // Drawing code CGContextRef context = UIGraphicsGetCurrentContext(); //调整坐标系 CGContextTranslateCTM(context, 0.0, self.bounds.size.height);//先垂直下移height高度 CGContextScaleCTM(context, 1.0, -1.0);//再垂直向上翻转 //绘制pdf内容 CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdfDocument, page); CGContextSaveGState(context); CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pageRef, kCGPDFCropBox, self.bounds, 0, true); CGContextConcatCTM(context, pdfTransform); CGContextDrawPDFPage(context, pageRef); CGContextRestoreGState(context); }
需要说明的是:
再放入UIPageViewController
中来展示,翻页效果杠杠的(具体效果见最后Demo)。
UIPageViewControllerde
感觉就像是一个书夹,其中每一页是一个UIViewController
,页面内容在UIViewController
的视图中绘制,翻页效果是自带的,你只要绘制好要展示的画面就好了;
在初始化UIPageViewControllerde
对象的时候,就要加载第一页,避免开始时出现空白页;
在下面方法中写上一页,下一页的调用逻辑;
//加载某一页调用方法 - (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion; //上一页视图控制器 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController //下一页视图控制器 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
UIPageViewControllerde
显示的内容没有缩放的功能,所以需要自己加。我是在UIViewController
视图里面嵌了一层UIScrollView
,设置其缩放系数minimumZoomScale, maximumZoomScale
,然后实现下面的代理方法来实现缩放的。- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
当然,这也可以用其他方法来完成,比如:UIPinchGestureRecognizer
MuPDF官网:http://www.mupdf.com/
git clone --recursive git://git.ghostscript.com/mupdf.git
;mupdf/platform/ios
目录下;mupdf/build/
,你会找到各个架构下的文件夹,里面包含了所有编译之后生成的包:lipo
命令去合并些架构下的依赖包,这将会生成比较大的包;使用lipo
命令合并.a包:
Last login: Mon Feb 29 20:43:50 on console promote:~ mac$ cd /Users/mac/Desktop/lib promote:lib mac$ lipo -create libmupdf_arm64.a libmupdf_x86_64.a -output libmupdf.a promote:lib mac$ lipo -info libmupdf.a Architectures in the fat file: libmupdf.a are: x86_64 arm64 promote:lib mac$ lipo -create libmupdfthird_arm64.a libmupdfthird_x86_64.a -output libmupdfthird.a promote:lib mac$ lipo -info libmupdfthird.a Architectures in the fat file: libmupdfthird.a are: x86_64 arm64 promote:lib mac$
mupdf/include/mupdf
路径下的所有头文件;mupdf/platform/ios/classes
路径下的所有obj-c类;mupdf/platform/ios
路径下的common.h,common .m
;Library Search Path
,添加依赖包的路径,$(inherited) $(PROJECT_DIR)/External/MuPDF/lib/
#include "mupdf/fitz.h"
,使用include
编译指令,所以头文件绝对路径=搜索路径+相对路径,所以需要配置搜索路径Header Search Paths
为"$(SRCROOT)/OpenPFDemo/ThirdLib/include"
;现在,你应该就可以运行你的程序了(具体的文件目录见Demo)。
mupdf/build/release-ios-armv7-arm64
路径下的依赖包;mupdf/build/debug-ios-arm64和debug-ios-x86_64
路径下的合并后的包;Library Search Path
中,分别配置Debug和Release
的路径;这样你在模拟器和真机Debug环境下使用的就是Debug对应的依赖包,而在打包的时候使用的就是Release对应的依赖包,而不会包含其他依赖包。
platform
文件夹下),所以比较大,要等完全下载完成之后再使用;pdf_write_document(ctx, idoc, tmp, &opts)
方法。解决方法:将MuDocumentController.m
中的pdf_write_document(ctx, idoc, tmp, &opts);
改为pdf_create_document(ctx);
即可;
enum { ResourceCacheMaxSize = 128<<20 /**< use at most 128M for resource cache */ }; queue = dispatch_queue_create("com.artifex.mupdf.queue", NULL); ctx = fz_new_context(NULL, NULL, ResourceCacheMaxSize); fz_register_document_handlers(ctx);
- (void)openMuPDF:(NSString *)path name:(NSString *)name { _filePath = malloc(strlen([path UTF8String])+1); strcpy(_filePath, [path UTF8String]); dispatch_sync(queue, ^{}); printf("open document '%s'\n", _filePath); _filename = [name retain]; doc = [[MuDocRef alloc] initWithFilename:_filePath]; if (!doc) { return; } if (fz_needs_password(ctx, doc->doc)) { [self askForPassword: @"'%@' needs a password:"]; } else { [self onPasswordOkay]; } } - (void)askForPassword: (NSString*)prompt { UIAlertView *passwordAlertView = [[UIAlertView alloc] initWithTitle: @"Password Protected" message: [NSString stringWithFormat: prompt, [_filename lastPathComponent]] delegate: self cancelButtonTitle: @"Cancel" otherButtonTitles: @"Done", nil]; [passwordAlertView setAlertViewStyle: UIAlertViewStyleSecureTextInput]; [passwordAlertView show]; [passwordAlertView release]; } - (void)onPasswordOkay { MuDocumentController *document = [[MuDocumentController alloc] initWithFilename:_filename path:_filePath document:doc]; if (document) { [self setTitle: @"Library"]; [[self navigationController] pushViewController:document animated:YES]; [document release]; } [_filename release]; free(_filePath); }
Demo下载链接:OpenPDFDemo