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:@""]) { //不能預覽
//做一些不能預覽的處理操作
}
;
}
