文件通常包括音频、视频、图片、办公等文档,如:.avi、.dat、.mkv、.flv、.vob、.mp4、.m4v、.mpg、.mpeg、.mpe、.3pg、.mov、.swf、.wmv、.asf、.asx、.rm、.rmvb、.wav、.aif、.au、.mp3、.ram、.wma、.mmf、.amr、.aac、.flac、.midi、.mp3、.oog、.cd、.asf、.rm、.real、.ape、.vqf、.jpg、.png、.jpeg、.gif、.bmp、.txt、.sh、.doc、.docx、.xls、.xlsx、.pdf、.hlp、.wps、.rtf、.html、.iso、.rar、.zip、.exe、.mdf、.ppt、.pptx。
使用QLPreviewController时
但常用的无非就是如下几类:.mp3、.mp4、.doc、.docx、.txt、.xls、.xlsx、.pdf、.ppt、.pptx、.png、.gif、.jpg、.jpeg。
在iOS研发中,可以根据情况进行对文档的查看操作。
情况1 txt文档格式时,可以直接读取文档内容,然后在UILabel上显示;
情况2 png等图片格式时,可以直接读取图片内容,然后在UIImageView上显示;
情况3 视频文件格式时,可以使用MPMoviePlayerViewControllert等视频播放器进行播放;
情况4 音频文件格式时,可以使用AVAudioPlayer等进行播放;
在实际开发中,除了根据不同格式文档选择不同的组件进行查看外,有些还可以通过统一的组件进行处理。
分别是使用UIWebView,或QLPreviewController,或UIDocumentInteractionController。
具体示例如下:
使用UIWebView时(音频文件不能播放)
- (void)fileReadWithFilePath:(NSString *)filePath target:(id)target
{
if (filePath && target)
{
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.controller = target;
if (self.webView.superview == nil)
{
self.webView.alpha = 1.0;
[self.controller.view addSubview:self.webView];
self.webView.frame = self.controller.view.bounds;
}
}
}
- (UIWebView *)webView
{
if (_webView == nil)
{
_webView = [[UIWebView alloc] init];
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return _webView;
}
- (void)dissmissWebView
{
self.webView.alpha = 1.0;
[UIView animateWithDuration:0.3 animations:^{
self.webView.alpha = 0.0;
} completion:^(BOOL finished) {
[self.webView removeFromSuperview];
}];
}
使用QLPreviewController时
#import <QuickLook/QuickLook.h>
- (void)fileReadWithFilePath:(NSString *)filePath target:(id)target
{
if (filePath && target)
{
NSURL *url = [NSURL fileURLWithPath:filePath];
if (self.filePathUrls == nil)
{
self.filePathUrls = [NSMutableArray array];
}
if (![self.filePathUrls containsObject:url])
{
[self.filePathUrls addObject:url];
}
self.controller = target;
[self.controller.navigationController pushViewController:self.fileController animated:YES];
}
}
- (QLPreviewController *)fileController
{
if (_fileController == nil)
{
_fileController = [[QLPreviewController alloc] init];
_fileController.dataSource = self;
_fileController.delegate = self;
}
return _fileController;
}
// QLPreviewControllerDataSource, QLPreviewControllerDelegate
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
return self.filePathUrls.count;
}
// 返回一个需要加载文件的URL
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
NSURL *url = self.filePathUrls[index];
return url;
}
使用UIDocumentInteractionController时
#import <QuickLook/QuickLook.h>
- (void)fileReadWithFilePath:(NSString *)filePath target:(id)target
{
if (filePath && target)
{
NSURL *url = [NSURL fileURLWithPath:filePath];
self.controller = target;
if (self.documentController == nil)
{
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:url];
self.documentController.delegate = self;
[self.documentController presentPreviewAnimated:YES];
}
}
}
// UIDocumentInteractionControllerDelegate
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self.controller;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
return self.controller.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller
{
return self.controller.view.bounds;
}
// 点击预览窗口的“Done”(完成)按钮时调用
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)_controller
{
self.documentController = nil;
}