iOS 文件預(yù)覽的四種方法

前言

開發(fā)中可能會遇到文件操作的需求,其中包括PDF文檔的預(yù)覽、圖片、doc文檔轉(zhuǎn)換成PDF文檔、第三方應(yīng)用導入文件置自己應(yīng)用中、PDF文檔的批注與修改。這篇文章我會先記錄預(yù)覽本地或網(wǎng)絡(luò)的文件(word、Excel、pdf、txt)等。

文檔預(yù)覽的幾種方式
  • UIWebView / WKWebView
  • QLPreviewController
  • UIDocumentInteractionController
  • CGContexDrawPDFPage

先看效果:


1786260-9123b5cdbd4dccfc.gif
UIWebView

蘋果的webView組件可以預(yù)覽各種格式的文件,支持在線預(yù)覽和本地預(yù)覽,相比于安卓的webView控件簡直就是一大神器,UIWebview使用起來也非常簡單

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"阿里巴巴java開發(fā)手冊" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:filePath];  
    _webView.scalesPageToFit = YES;
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
QLPreviewController

QLPreviewController是系統(tǒng)自帶的文件預(yù)覽控制器,QL全稱quick look快速查看的意思,要使用QLPreviewController先得在文件中導入頭文件#import <QuickLook/QuickLook.h>,并且實現(xiàn)其代理方法QLPreviewControllerDelegate

- (QLPreviewController *)qlVc{
    if (!_qlVc) {
        _qlVc = [[QLPreviewController alloc] init];
        _qlVc.delegate = self;
    }return _qlVc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *filePath1 = [[NSBundle mainBundle] pathForResource:@"阿里巴巴java開發(fā)手冊" ofType:@"pdf"];
    NSString *filePath2 = [[NSBundle mainBundle] pathForResource:@"excel操作大全" ofType:@"doc"];
    NSString *filePath3 = [[NSBundle mainBundle] pathForResource:@"H5,JS資源" ofType:@"txt"];
    NSString *filePath4 = [[NSBundle mainBundle] pathForResource:@"Xcode快捷鍵" ofType:@"rtf"];
    NSString *filePath5 = [[NSBundle mainBundle] pathForResource:@"華為推薦書目" ofType:@"xls"];
    NSString *filePath6 = [[NSBundle mainBundle] pathForResource:@"大話Swift 3.0(上)" ofType:@"key"];
    NSString *filePath7 = [[NSBundle mainBundle] pathForResource:@"抵押貸款" ofType:@"numbers"];
    NSString *filePath8 = [[NSBundle mainBundle] pathForResource:@"Page" ofType:@"pages"];
    NSString *filePath9 = [[NSBundle mainBundle] pathForResource:@"iOS" ofType:@"ppt"];
    NSString *filePath10 = [[NSBundle mainBundle] pathForResource:@"Beyond - 真的愛你" ofType:@"mp3"];
    NSString *filePath11 = [[NSBundle mainBundle] pathForResource:@"aa" ofType:@"jpg"];
    NSString *filePath12 = [[NSBundle mainBundle] pathForResource:@"52CB015AA5D0A2330C59C0600469CA8F2" ofType:@"mp4"];
    
    _files = @[filePath1,filePath2,filePath3,filePath4,filePath5,filePath6,filePath7,filePath8,filePath9,filePath10,filePath11,filePath12];
    
// push跳轉(zhuǎn)預(yù)覽頁面 也可以present
    [self.navigationController pushViewController:self.qlVc animated:YES];

}
#pragma mark QLPreviewControllerDataSource
//返回文件的個數(shù)
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return _files.count;
}

//加載需要顯示的文件 
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
    
    return [NSURL fileURLWithPath:_files[index]];
}
  • QLPreviewController只能加載本地文件,不支持在線預(yù)覽。顯示效果比webView要好。
  • 以前認為QLPreviewController只能加載PDF、word之類的文件,沒想到也能加載本地音視頻文件
  • 支持多文件預(yù)覽,支持橫滑切換
  • 系統(tǒng)自帶分享功能,可以在預(yù)覽的同時將文件分享出去
UIDocumentInteractionController

使用UIDocumentInteractionController預(yù)覽文件也得遵循UIDocumentInteractionControllerDelegate代理方法,UIDocumentInteractionController本身并不是一個控制器類,它直接繼承NSObject,所以就不能直接push或者模態(tài)跳轉(zhuǎn)了,所以需要使用它類方法提供的模態(tài)跳轉(zhuǎn)函數(shù)

_docVc = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:_files[0]]];
     _docVc.delegate = self;
    [_docVc presentPreviewAnimated:YES];

#pragma mark -- UIDocumentInteractionControllerDelegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
    return self;
}
- (UIView*)documentInteractionControllerViewForPreview:(UIDocumentInteractionController*)controller
{
    return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController*)controller
{
    return self.view.frame;
}
  • 預(yù)覽效果和QLPreviewController一樣,但不支持多文件預(yù)覽
  • 同樣支持文件共享功能,在預(yù)覽之前可以選擇先跳轉(zhuǎn)預(yù)覽頁面還是先彈出分享面板。
CGContexDrawPDFPage

利用CGContexDrawPDFPageUIPageViewController實現(xiàn)翻頁瀏覽功能,這種方法只能加載本地的PDF文件,但是顯示效果比其他幾種炫酷很多。具體使用方法參考iOS開發(fā)筆記——PDF的顯示和瀏覽這篇博客。也可以參考本文的Demo

原文地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容