ios本地文件的预览有一下三种方法:
1.UIDocumentInteractionController
UIDocumentInteractionController是苹果自带的文件预览控制器,自ios3就出现了,知识我们没注意而已,并且自带用第三方打开的功能;,用法具体如下
NSURL *fileRUL = [NSURL fileURLWithPath:path]; //path:本地文件绝对路径
UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileRUL];
documentInteractionController.delegate = self;
//预览有其他软件打开按钮
// [documentInteractionController presentPreviewAnimated:YES];
CGRect navRect =self.navigationController.navigationBar.frame;
navRect.size =CGSizeMake(1500.0f,40.0f);
//直接显示包含预览的菜单项
// [documentInteractionController presentOptionsMenuFromRect:navRect inView:self.view animated: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;
}
2.quickLook预览文件,
quickLook预览文件也是系统提供的预览方法,具体使用如下
首先导入#import <QuickLook/QuickLook.h>库
使用代码如下
QLPreviewController *myQlPreViewController = [[QLPreviewController alloc]init];
myQlPreViewController.delegate =self;
myQlPreViewController.dataSource =self;
[myQlPreViewController setCurrentPreviewItemIndex:0];
[self presentViewController:myQlPreViewController animated:YES completion:nil];
实现代理方法和数据源方法
#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return 1;
}
- (id)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
return [NSURL fileURLWithPath:self.fileModel.fileAbsolutePath]; //返回文件路径
}
3,用功能强大的webview来实现文件预览功能
NSURL *fileRUL = [NSURL fileURLWithPath:self.fileModel.fileAbsolutePath];
UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame];
webView.delegate = self;
webView.multipleTouchEnabled = YES;
webView.scalesPageToFit = YES;
webView.scrollView.bounces = NO;
//加载动画
[self bsShowLoadingView];
[self.view addSubview:webView];
NSStringEncoding *useEncodeing = nil;
//排除一下用webview预览文件出现乱码情况
//带编码头的如utf-8等,
NSString *body = [NSString stringWithContentsOfFile:self.fileModel.fileAbsolutePath usedEncoding:useEncodeing error:nil];
//识别不到,按GBK编码再解码一次.这里不能先按GB18030解码,否则会出现整个文档无换行bug。
if (!body) {
body = [NSString stringWithContentsOfFile:self.fileModel.fileAbsolutePath encoding:0x80000632 error:nil];
}
//还是识别不到,按GB18030编码再解码一次.
if (!body) {
body = [NSString stringWithContentsOfFile:self.fileModel.fileAbsolutePath encoding:0x80000631 error:nil];
}
//展现
if (body) {
body =[body stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];//替换换行符为HTML换行符
[webView loadHTMLString:body baseURL:fileRUL];
}else { //其他文件直接加载请求
NSURLRequest *request = [NSURLRequest requestWithURL:fileRUL];
[webView loadRequest:request];
}
然后监听一下webview加载完成的时候,根据webview是否展示内容,来判断是否预览成功
#pragma mark- webViewDelegate
//加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//获取加载的h5字符串的body
NSString *lJs = @"document.documentElement.innerText";//获取当前网页的html
NSString *currentHTMLBody = [webView stringByEvaluatingJavaScriptFromString:lJs];
if ([currentHTMLBody isEqualToString:@""]) { //不能预览
//做一些不能预览的处理操作
}
;
}