谈谈预览文档和其它多种格式文件的实现方法

来源:互联网 发布:政府大数据解决方案 编辑:程序博客网 时间:2024/06/05 06:09

终于可以将我这几天的努力写在亲爱的博客上了。将前后研究的一起总结一下,现在可以使用三种方式进行预览控制,第一种与第二种都是使用QLPreviewController,第二种是用UIDocumentInteractionControllerDelegate,都属于QuickLook框架。具体详细说吧:

一、

利用QLPreviewControllerDataSource进行文件预览。

前面我写了一篇《

第一个实用程序---多格式阅读器制作》,

实现的方法是将数据源设为self:

        QLPreviewController *previewer = [[QLPreviewController alloc] init];        
        [previewer setDataSource:self];

        [previewer setCurrentPreviewItemIndex:indexPath.row];

        [[self navigationController] pushViewController:previewer animated:YES];

然后通过以下两个委托方法进行预览控制:
       - (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{return [self.documentFilenames count];}
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{             。。。。
return [NSURL fileURLWithPath:bookPath];}
用这种方法的好处是你给的一个NSURL地址,预览控制器将这份文档所在目录里的所有文件都加载到控制器,所以可以实现横向手势的翻动,不必再退回上一级面板进行重新选择。但是有一个问题就是,预览窗口中会包括一个标题,显示文件 URL 的最后一段路径,文件预览里的导航栏我没有办法控制,因为我需要修改标题,用以下这两句都不行:

        [self.previewer setTitle:@"tt"];
        self.previewer.navigationItem.title=@"tt";

后来我理解,光这样做不行的原因是,你这个设置的这个标题导航并不知道是给哪一个预览的文件设的。后面附件里面有一句标红的话“预览窗口中会包括一个标题,显示文件 URL 的最后一段路径。如果要重载标题,可以定制PreviewItem 类,并实现QLPreviewItem 协议中的 previewItemTitle方法。”,说明是可以实现的,我还没有找到方法。但这个在我的ipad上用这个做的资料管理的软件很好用!

二、利用QLPreviewControllerDataSource进行文件预览。但实现的数据源不一样。
这里可以给数据源单独的一个URL,预览器就只用加载这一个文件进行预览。
但要先定义一个继承于NSObject的类,并使用了QLPreviewControllerDataSource接口。头文件如下
#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>
@interface BZPreviewDataSource : NSObject<QLPreviewControllerDataSource>
@property (nonatomic, retain) NSString *path;
@end

.m文件如下:
#import "BZPreviewDataSource.h"
@implementation BZPreviewDataSource
@synthesize path = _path;
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{    return 1;}
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{ return [NSURL fileURLWithPath:_path];} 
@end

然后在你需要使用的控制类里面用如下代码:

       self.previewoCntroller = [[QLPreviewController alloc] init];
        BZPreviewDataSource *dataSource = [[BZPreviewDataSource alloc]init];
        dataSource.path = [[NSString alloc] initWithString:KnowledgeFullPath];
          self.previewoCntroller.dataSource = dataSource;
        [self.previewoCntroller setDelegate:self];
       float version = [[[UIDevice currentDevice] systemVersion] floatValue];
        if (version >= 5.0){
            //此函数是5.0之后的函数。
            [self presentViewController:self.previewoCntroller animated:YES completion:nil];}

      else { [self.navigationController pushViewController:self.previewoCntroller animated:YES];
     }


三、使用

UIDocumentInteractionControllerDelegate。

代码非常简单,而且可控,唯一缺点是,利用缺省参数一次只能预览一个。

先对UIDocumentInteractionController做一个了解:
iOS中的沙盒可以让平台更加的安全,这也是沙盒给用户带来的最主要好处。不过由于沙盒的严格限制,导致程序之间共享数据比较麻烦。一般在程序间共享文档可以通过UIDocumentInteractionController(该类经常被开发者忽略)。本文中,我将介绍如何使用这个类在其它程序(已经安装在设备中的程序)中预览和打开文档。
 UIDocumentInteractionController在iOS 3.2中就已经存在了,使用起来非常灵活,功能也比较强大。它除了支持同设备上app之间的文档分享外,还可以实现文档的预览、打印、发邮件以及复制。
 UIDocumentInteractionController的使用非常简单。首先通过调用它唯一的类方法interactionControllerWithURL:,并传入一个URL(NSURL),来初始化一个实例对象。之后设置一下这个view controller的delegate属性,并实现一些恰当的delegate方法。
 注意,UIDocumentInteractionController并不是UIViewController的子类,所以必须通知document interaction controller使用哪个view controller来预览文档。
具体实现是:
1、加#import <QuickLook/QuickLook.h>,并加接口UIDocumentInteractionControllerDelegate
2、在.m里定义一个对象@property (strong, nonatomic) UIDocumentInteractionController *documentInteractionController;
3、在需要使用的方法(这里是didSelectRowAtIndexPath)里添加代码:
      self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
        // Configure Document Interaction Controller
        [self.documentInteractionController setDelegate:self];
        self.documentInteractionController.name=@"test";//这里就可以自定义控制栏上的标题了
         // Preview PDF
        [self.documentInteractionController presentPreviewAnimated:YES];

4、重载一个委托的方法:
    - (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
    {return [self.documentFilenames count];}
解释:UIDocumentInteractionControllerDelegate中有一个名documentInteractionControllerViewControllerForPreview:的delegate方法,该方法请求获得一个用于显示(预览)文档的view controller。我们希望在main view controller中显示预览,所以可简单的返回self,如下代码所示。它的意思是document interfation controller将使用我们的view controller来预览PDF文档——以modal view的方式显示文档。
好了,这个方法更简单,但如果要实现像前面那样的功能还要做一些工作。


官方文档,以作参考:

使用 Quick Look 框架

 Quick Look 框架提供了增强的预览功能。该框架主要提供了 QLPreviewController 类。该类依赖于委托对象响应预览动作,以及一个用于提供预览文件的数据源。

从 iOS 4.2 开始,QuickLook preview controller 提供了包含了一个 action 按钮(即打印按钮)的预览视图。对于 controller 能预览的文件,action按钮能够打印该文档。从而不需要你编写任何打印代码。

通过以下方式显示一个Quick Look preview controller:

·      通过导航控制器,将预览窗口以“push 方式”显示。

·      通过 UIViewController 的 presentModalViewController:animated:方法以模态窗口的方式显示。

·      显示一个document interaction controller(如 “预览及打开文件” 中所述)。用户可以从document interaction controller 的选项菜单中选择“Quick Look”,即可打开一个 QuickLook preview controller。

显示 Quick Lookpreview controller 时,请选择适合于你的应用程序的外观和导航方式。如果你的程序未使用导航条,使用一个全屏的模态的 Quick Lookpreview controller 是合适的。如果你的程序使用了“iPhone 式”的导航,则采用 push 方式呈现预览窗口是合适的。

预览窗口中会包括一个标题,显示文件 URL 的最后一段路径。如果要重载标题,可以定制PreviewItem 类,并实现QLPreviewItem 协议中的 previewItemTitle方法。

Quick Look previewcontroller 能够预览下列文件:

·      iWork 文档

·      Microsoft Office 文档(Office ‘97 以后版本)

·      Rich Text Format (RTF) 文档

·      PDF 文档

·      图片

·      文本文件,其 uniform type identifier (UTI)  在 public.text 文件中定义 (查看UniformType Identifiers 参考)

·      Comma-separated value (csv) 文件

使用 QuickLook preview controller,必须指定数据源对象(即实现 QLPreviewControllerDataSource 协议,请查看QLPreviewControllerDataSource协议参考)。数据源为 Quick Look preview controller 提供预览对象(preivew item),及指明它们的数量以便在一个预览导航列表中包含它们。在这个列表中包含多个对象,在模态预览窗口(全屏显示)显示了导航箭头,以便用户在多个预览对象间切换。对于用导航控制器“push方式”显示的QuickLook preview controller,你可以在导航条上提供按钮以便在预览对象列表见切换。



0 0
原创粉丝点击