iOS中多种格式文档的查看浏览

文件通常包括音频、视频、图片、办公等文档,如:.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。

但常用的无非就是如下几类:.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;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值