由于之前需求用的是福昕SDK做的PDF預(yù)覽與批注握础,也特別感謝大神的幫助iOS PDF文檔批注與修改桥言,由于福昕SDK不是免費(fèi)開(kāi)源的,后來(lái)查找各種資料與博客總結(jié)了MuPDF集成步驟及說(shuō)明
一、首先去官網(wǎng)下載開(kāi)源代碼
網(wǎng)址:http://www.mupdf.com(本文使用為1.6版本)
二苫拍、生產(chǎn)依賴(lài)包睦袖,解壓下載完成文件珊肃,進(jìn)入mupdf/platform/ios目錄下;
1.打開(kāi)MuPDF工程馅笙,修改Build Configuration 使用真機(jī)和模擬器 運(yùn)行Relese和debug版本生產(chǎn).a文件伦乔。
2,分別在各版本下選擇模擬器和真機(jī)運(yùn)行項(xiàng)目董习,會(huì)在mupdf下生產(chǎn)build目錄烈和,選擇你需要的依賴(lài)包文件。(如果正常編譯出.a文件則下面3可以忽略皿淋,如編譯出錯(cuò)可參考如下)
3招刹,運(yùn)行項(xiàng)目時(shí)可能Xcode會(huì)報(bào)錯(cuò) :make: *** [build/debug/cmapdump.o] Error ?
make generate此時(shí)需要下載安裝Command Line Tools,下載地址(此處可能需要登錄開(kāi)發(fā)者賬號(hào)下載):https://developer.apple.com/downloads/index.action#
這次 make generate 成功了窝趣!可以編譯出.a文件了疯暑,
4,合成支持全部架構(gòu)的通用靜態(tài)庫(kù)
在終端輸入命令: lipo -create /Users/ssiwo02/Desktop/A/libXXXXX-armv7.a ?/Users/ssiwo02/Desktop/B/libXXXXXX-armv7s.a /Users/ssiwo02/Desktop/C/libXXXXXX-i386.a ?/Users/ssiwo02/Desktop/D/libXXXXXX-x86_64.a /Users/ssiwo02/Desktop/E/libXXXXXX- arm64.a ?-output ?/Users/ssiwo02/Desktop/libXXXXX.a
執(zhí)行完畢,就會(huì)在桌面上生成一個(gè)命名為libXXXXX.a哑舒,這個(gè)libXXXXXX.a就是我們所需要的.a庫(kù)妇拯。
三、開(kāi)始集成
1洗鸵、添加所需要的源碼文件:
1.0越锈、導(dǎo)入mupdf/include路徑下的所有頭文件宣赔;
1.1、導(dǎo)入mupdf/platform/ios/classe路徑下的所有obj-c類(lèi)瞪浸;需要改為混編模式 對(duì)應(yīng)項(xiàng) 改為-fno-objc-arc
1.2儒将、導(dǎo)入mupdf/platform/ios路徑下的common.h,common .m对蒲;
1.3钩蚊、導(dǎo)入mupdf/platform/android/res/drawable-ldpi;資源圖片(如果不導(dǎo)入可能不會(huì)顯示功能操作按鈕,可以后面自己做圖片換上)
2蹈矮、添加之前生成好的依賴(lài)包到你的工程中砰逻;
由于Xcode9把文件添加后好像不會(huì)自動(dòng)引入有時(shí)需要手動(dòng)勾選
3、配置你工程的Library Search Path泛鸟,添加依賴(lài)包的路徑蝠咆,
比如:$(inherited) $(PROJECT_DIR)/External/MuPDF/lib/(可以直接選擇文件拖入配置列表中)
4、由于庫(kù)中引用文件#include "mupdf/fitz.h"北滥,使用include編譯指令刚操,所以頭文件絕對(duì)路徑=搜索路徑+相對(duì)路徑,(否則會(huì)報(bào)錯(cuò) "mupdf/fitz.h" NotFount)
所以需要配置搜索路徑Header Search Paths為"$(SRCROOT)/OpenPFDemo/ThirdLib/include"再芋;
5菊霜、現(xiàn)在,你應(yīng)該就可以運(yùn)行你的程序了济赎,哦對(duì)了運(yùn)行時(shí)可能會(huì)報(bào)錯(cuò)如下
Undefined symbols for architecture arm64:
"_fz_authenticate_password", referenced from:
-[MuLibraryController alertView:clickedButtonAtIndex:] in MuLibraryController.o
此時(shí)到build settings—>Enable bitcode 改為No就好了鉴逞;此時(shí)項(xiàng)目集成完成
6、調(diào)用打開(kāi)PDF方法
1司训、APPDelegate.h中
引入頭文件:
#include "common.h"
#include "mupdf/fitz.h"
enum {
// use at most 128M for resource cache
ResourceCacheMaxSize = 128<<20? ? // use at most 128M for resource cache
};
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *filename;
queue = dispatch_queue_create("com.artifex.mupdf.queue", NULL);
ctx = fz_new_context(NULL, NULL, ResourceCacheMaxSize);
fz_register_document_handlers(ctx);
}
2.調(diào)用打開(kāi)方法
#include "common.h"
#import "MuDocumentController.h"
#import "MuDocRef.h"
//(可以刪除項(xiàng)目中導(dǎo)入的mupdf/platform/ios/classes下的MuLibraryController類(lèi))
@interface ViewController () {
char *tempPATH;
}
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"" ofType:@"pdf"];? ? ? //文件路徑
tempPATH = malloc(strlen([filepath UTF8String])+1);
strcpy(tempPATH, [filepath UTF8String]);
MuDocRef *doc = [[MuDocRef alloc] initWithFilename: tempPATH];
MuDocumentController *document = [[MuDocumentController alloc] initWithFilename: @"123.pdf" path:tempPATH document: doc];
if (document) {
[[self navigationController] pushViewController: document animated: YES];
}
pdf顯示界面調(diào)整可在MuDocumentController類(lèi)中自己根據(jù)需求修改构捡, 至此結(jié)束!