之前的老項(xiàng)目遇到了一個(gè)問(wèn)題:iOS9后PDF中文會(huì)顯示亂碼,而且試了各種方法都不行,還好最終找到了解決方法---MuPDF
在此總結(jié)一下解決該問(wèn)題過(guò)程中使用過(guò)的方法:
1.使用UIWebView加載
//代碼很簡(jiǎn)單
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];
2.使用QLPreviewController打開(kāi)
//初始化QLPreviewController對(duì)象
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
[self.navigationController pushViewController:pageController animated:YES];
//再實(shí)現(xiàn)QLPreviewControllerDataSource的兩個(gè)方法即可顯示
//顯示文件數(shù)量
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller;
//文件路徑URL
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index;
3.使用CGContextDrawPDFPage繪制
在UIView
的- (void)drawRect:(CGRect)rect
方法中繪制PDF內(nèi)容,代碼如下:
/*
Quartz2D UIKit
y (0, 0)|----------x
| |
| |
| |
| |
(0, 0) |---------x y
*/
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
//調(diào)整坐標(biāo)系
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);//先垂直下移height高度
CGContextScaleCTM(context, 1.0, -1.0);//再垂直向上翻轉(zhuǎn)
//繪制pdf內(nèi)容
CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdfDocument, page);
CGContextSaveGState(context);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pageRef, kCGPDFCropBox, self.bounds, 0, true);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, pageRef);
CGContextRestoreGState(context);
}
需要說(shuō)明的是:
- Quartz 2D坐標(biāo)系的原點(diǎn)在界面左下角,而UIKit坐標(biāo)系的原點(diǎn)在左上角,所以需要對(duì)畫(huà)布進(jìn)行翻轉(zhuǎn),才能得到正確的視圖;
- 方法中需要傳入page和pdfDocument,CGContextDrawPDFPage是按照頁(yè)碼來(lái)一張張繪制的;
再放入UIPageViewController
中來(lái)展示,翻頁(yè)效果杠杠的(具體效果見(jiàn)最后Demo)。
順便說(shuō)說(shuō)UIPageViewController的使用
UIPageViewControllerde
感覺(jué)就像是一個(gè)書(shū)夾,其中每一頁(yè)是一個(gè)UIViewController
,頁(yè)面內(nèi)容在UIViewController
的視圖中繪制,翻頁(yè)效果是自帶的,你只要繪制好要展示的畫(huà)面就好了;在初始化
UIPageViewControllerde
對(duì)象的時(shí)候,就要加載第一頁(yè),避免開(kāi)始時(shí)出現(xiàn)空白頁(yè);在下面方法中寫(xiě)上一頁(yè),下一頁(yè)的調(diào)用邏輯;
//加載某一頁(yè)調(diào)用方法
- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;
//上一頁(yè)視圖控制器
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
//下一頁(yè)視圖控制器
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
-
UIPageViewControllerde
顯示的內(nèi)容沒(méi)有縮放的功能,所以需要自己加。我是在UIViewController
視圖里面嵌了一層UIScrollView
,設(shè)置其縮放系數(shù)minimumZoomScale, maximumZoomScale
,然后實(shí)現(xiàn)下面的代理方法來(lái)實(shí)現(xiàn)縮放的。
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
當(dāng)然,這也可以用其他方法來(lái)完成,比如:UIPinchGestureRecognizer
4.MuPDF類庫(kù)的使用
MuPDF官網(wǎng):http://www.mupdf.com/
4.1 生成依賴包
- 源碼下載:
git clone --recursive git://git.ghostscript.com/mupdf.git
; - 下載完成后,進(jìn)入
mupdf/platform/ios
目錄下; - 打開(kāi)MuPDF工程;
- 修改工程的Build Configuration為Release;
- 選擇iPhone模擬器,運(yùn)行這個(gè)工程,這將生成i386或x86_64結(jié)構(gòu)的.a包(根據(jù)你選擇的模擬器類型);
- 連接你的iPhone,修改你自己的bundle identifier, certificate和 provisioning profile,在真機(jī)環(huán)境下運(yùn)行該程序,這將會(huì)生成armv7,arm64架構(gòu)的.a包;
- 同理,修改工程的Build Configuration為Debug,可以生成Debug對(duì)應(yīng)的.a包;
- 進(jìn)入
mupdf/build/
,你會(huì)找到各個(gè)架構(gòu)下的文件夾,里面包含了所有編譯之后生成的包: - 現(xiàn)在,你可以使用
lipo
命令去合并些架構(gòu)下的依賴包,這將會(huì)生成比較大的包;
使用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$
- 我只合并了debug中的arm64和x86_64的包,要是再合并i386就太大了。
4.2 開(kāi)始集成
- 添加所需要的源碼文件:
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/
- 由于庫(kù)中引用文件
#include "mupdf/fitz.h"
,使用include
編譯指令,所以頭文件絕對(duì)路徑=搜索路徑+相對(duì)路徑,所以需要配置搜索路徑Header Search Paths
為"$(SRCROOT)/OpenPFDemo/ThirdLib/include"
;
現(xiàn)在,你應(yīng)該就可以運(yùn)行你的程序了(具體的文件目錄見(jiàn)Demo)。
4.3 提示:
- 由于合并之后的依賴包比較大,所以為了減少你App的大小,你可以配置兩個(gè)路徑:
在release文件夾下添加mupdf/build/release-ios-armv7-arm64
路徑下的依賴包;
在debug文件夾下添加mupdf/build/debug-ios-arm64和debug-ios-x86_64
路徑下的合并后的包;
- 在你的工程配置
Library Search Path
中,分別配置Debug和Release
的路徑;
這樣你在模擬器和真機(jī)Debug環(huán)境下使用的就是Debug對(duì)應(yīng)的依賴包,而在打包的時(shí)候使用的就是Release對(duì)應(yīng)的依賴包,而不會(huì)包含其他依賴包。
4.4 注意
- 按照 stackoverflow上面的這個(gè)問(wèn)答的集成步驟在當(dāng)前最新的MuPDF版本上集成時(shí)會(huì)有些出入,所以我修改了一下;
- 由于這個(gè)庫(kù)包含了很多平臺(tái)(見(jiàn)
platform
文件夾下),所以比較大,要等完全下載完成之后再使用; - 下載完成之后,運(yùn)行其中的iOS代碼,發(fā)現(xiàn)居然報(bào)錯(cuò)了(我用的xcode7.0)
原因是找不到pdf_write_document(ctx, idoc, tmp, &opts)
方法。
解決方法:將MuDocumentController.m
中的pdf_write_document(ctx, idoc, tmp, &opts);
改為pdf_create_document(ctx);
即可;
4.6 使用打開(kāi)PDF
- 首先,在程序啟動(dòng)后,初始化相關(guān)參數(shù):
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);
- 然后調(diào)用下面方法打開(kāi):
- (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