ios实用开发技巧

iOS 文档预览的四种方法

2018-09-13  本文已影响186人  彗星来的那一夜

前言

前段时间公司项目加了许多关于PDF文件操作的需求,其中包括PDF文档的预览图片、doc文档转换成PDF文档第三方应用导入文件置自己应用中PDF文档的批注与修改。这篇文章我会先记录下PDF文档的几种预览方式。

PDF文档预览的几种方式

UIWebView

苹果的webView组件可以预览各种格式的文件,支持在线预览和本地预览,相比于安卓的webView控件简直就是一大神器,UIWebview使用起来也非常简单

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"阿里巴巴java开发手册" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:filePath];  
    _webView.scalesPageToFit = YES;
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];

QLPreviewController

QLPreviewController是系统自带的文件预览控制器,QL全称quick look快速查看的意思,要使用QLPreviewController先得在文件中导入头文件#import <QuickLook/QuickLook.h> ,并且实现其代理方法QLPreviewControllerDelegate

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

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *filePath1 = [[NSBundle mainBundle] pathForResource:@"阿里巴巴java开发手册" 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跳转预览页面 也可以present
    [self.navigationController pushViewController:self.qlVc animated:YES];

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

//加载需要显示的文件 
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{

    return [NSURL fileURLWithPath:_files[index]];
}

UIDocumentInteractionController

使用UIDocumentInteractionController预览文件也得遵循UIDocumentInteractionControllerDelegate代理方法,UIDocumentInteractionController本身并不是一个控制器类,它直接继承NSObject,所以就不能直接push或者模态跳转了,所以需要使用它类方法提供的模态跳转函数

 _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;
}

CGContexDrawPDFPage

利用CGContexDrawPDFPageUIPageViewController实现翻页浏览功能,这种方法只能加载本地的PDF文件,但是显示效果比其他几种炫酷很多。具体使用方法参考iOS开发笔记——PDF的显示和浏览这篇博客。

上一篇下一篇

猜你喜欢

热点阅读