本文转载至 http://blog.csdn.net/devday/article/details/6580444
 

ios 4 sdk中支技文档的预览功能,何为预览?就是你打印文件时的预览功能。其用到quicklook.framework,它支持的文档格式有: iWork documents, Microsoft Office, Rich Text Format, PDF, images, text files and comma-separated (csv) files.

今天show一个demo,展示其用法:

第一步:创建一个基于view的工程,并加入quicklook.framewrok

第二步:修改Controller的头文件如下:

  1. #import <QuickLook/QuickLook.h>
  2. @interface TestViewController : UITableViewController <QLPreviewControllerDataSource>
  3. {
  4. NSArray *arrayOfDocuments;
  5. }
  6. @end

修改 controller执行文件如下

  1. #import "TestViewController.h"
  2. @implementation TestViewController
  3. #pragma mark -
  4. #pragma mark Initialization
  5. /*---------------------------------------------------------------------------
  6. *
  7. *--------------------------------------------------------------------------*/
  8. -(id)init
  9. {
  10. if (self = [super init])
  11. {
  12. arrayOfDocuments = [[NSArray alloc] initWithObjects:
  13. @"iOSDevTips.png", @"Remodel.xls", @"Core J2ME Technology.pdf", nil];
  14. }
  15. return self;
  16. }
  17. /*---------------------------------------------------------------------------
  18. *
  19. *--------------------------------------------------------------------------*/
  20. - (void)loadView
  21. {
  22. [super loadView];
  23. [self setTitle:@"Files Available for Preview"];
  24. }
  25. #pragma mark -
  26. #pragma mark Table Management
  27. // Customize the number of sections in the table view.
  28. /*---------------------------------------------------------------------------
  29. *
  30. *--------------------------------------------------------------------------*/
  31. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  32. {
  33. return 1;
  34. }
  35. /*---------------------------------------------------------------------------
  36. *
  37. *--------------------------------------------------------------------------*/
  38. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  39. {
  40. return [arrayOfDocuments count];
  41. }
  42. /*---------------------------------------------------------------------------
  43. *
  44. *--------------------------------------------------------------------------*/
  45. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  46. {
  47. static NSString *CellIdentifier = @"tableRow";
  48. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  49. if (cell == nil)
  50. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  51. // ???
  52. [[cell textLabel] setText:[arrayOfDocuments objectAtIndex:indexPath.row]];
  53. [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
  54. return cell;
  55. }
  56. /*---------------------------------------------------------------------------
  57. *
  58. *--------------------------------------------------------------------------*/
  59. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  60. {
  61. // When user taps a row, create the preview controller
  62. QLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];
  63. // Set data source
  64. [previewer setDataSource:self];
  65. // Which item to preview
  66. [previewer setCurrentPreviewItemIndex:indexPath.row];
  67. // Push new viewcontroller, previewing the document
  68. [[self navigationController] pushViewController:previewer animated:YES];
  69. }
  70. #pragma mark -
  71. #pragma mark Preview Controller
  72. /*---------------------------------------------------------------------------
  73. *
  74. *--------------------------------------------------------------------------*/
  75. - (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
  76. {
  77. return [arrayOfDocuments count];
  78. }
  79. /*---------------------------------------------------------------------------
  80. *
  81. *--------------------------------------------------------------------------*/
  82. - (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
  83. {
  84. // Break the path into it's components (filename and extension)
  85. NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];
  86. // Use the filename (index 0) and the extension (index 1) to get path
  87. NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];
  88. return [NSURL fileURLWithPath:path];
  89. }
  90. #pragma mark -
  91. #pragma mark Cleanup
  92. /*---------------------------------------------------------------------------
  93. *
  94. *--------------------------------------------------------------------------*/
  95. - (void)dealloc
  96. {
  97. // Free up all the documents
  98. [arrayOfDocuments release];
  99. [super dealloc];
  100. }
  101. @end

修改Appdelegate如下

  1. - (void)applicationDidFinishLaunching:(UIApplication *)application
  2. {
  3. // Create and initialize the window
  4. window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  5. // Create test view controller
  6. vc = [[TestViewController alloc] init];
  7. // Create navigation controller
  8. nav = [[UINavigationController alloc] initWithRootViewController:vc];
  9. [window addSubview:[nav view]];
  10. [window makeKeyAndVisible];
  11. }

所要的资源文件可以源码中找到。

iOS文档预览功能教程的更多相关文章

  1. 一文带你玩转对象存储COS文档预览

    随着"互联网+"的发展,各行各业纷纷"去纸化",商务合同.会议纪要.组织公文.商品图片.培训视频.学习课件.随堂讲义等电子文档无处不在.而要查看文档一般需要先下 ...

  2. 秒级接入、效果满分的文档预览方案——COS文档预览

    一.导语 ​ 说起 Microsoft Office 办公三件套,想必大家都不会陌生,社畜日常的工作或者生活中,多多少少遇到过这种情况: 本地创建的文档换一台电脑打开,就出现了字体丢失.排版混乱的情况 ...

  3. 在线文档预览方案-office web apps续篇

    上一篇在线文档预览方案-office web apps发布后收到很多网友的留言提问,所以准备再写一篇,一来介绍一下域控服务器安装,总结一下大家问的多的问题,二来宣传预览服务安装与技术支持的事情. 阅读 ...

  4. 在线文档预览方案-office web apps

    最近在做项目时,要在手机端实现在线文档预览的功能.于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览.这些方案没有具体实现代码,也没有在线预览的地址,再加上项目时间紧迫.只能考虑 ...

  5. [转载]在线文档预览方案-Office Web Apps

    最近在做项目时,要在手机端实现在线文档预览的功能.于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览.这些方案没有具体实现代码,也没有在线预览的地址,再加上项目时间紧迫.只能考虑 ...

  6. 微软office web apps 服务器搭建之在线文档预览(一)

    office web apps安装 系统要求为Windows Server 2012, 注意:转换文档需要两台服务器,一台为转换server,另外一台为域控server.(至于为什么要两台,这个请自行 ...

  7. 使用OpenOffice实现文档预览

    概述 使用OpenOffice将 office文档转为pdf,然后再将pdf转为图片,实现文档预览的功能. 依赖组件 OpenOffice.org或者LibreOffice JODConverter ...

  8. 微软office web apps 服务器搭建之在线文档预览(二)

    上一篇文章已经介绍了整个安装过程了.只要在浏览器中输入文档转换server的ip,会自动跳转,出现如下页面. 那么就可以实现本地文档预览了,你可以试试.(注意:是本地哦,路径不要写错,类似“\\fil ...

  9. 解决officeOnline文档预览服务器只能域名提交的限制Redirect

    此项目是解决officeOnline文档预览只能用域名提交的限制 http://officeOnline文档预览域名或IP/op/generate.aspx // 微软原生页面 创建链接后会生成全屏预 ...

随机推荐

  1. win7 64位数据库连接问题:在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配

    通过ODBC/JDBC连接程序和数据库时,异常报错“在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配”的解决办法 本机:64位win7,64位JDK,做Web实验用32位myeclipse ...

  2. hihoCoder #1586 : Minimum-结构体版线段树(单点更新+区间最值求区间两数最小乘积) (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1586 : Minimum Time Limit:1000ms Case Time Limit:1000ms Memory Limit:256MB Description You are give ...

  3. 4C 2018 福到了

    输入字符c(只含有@和空格).数字n.规模n*n的二维字符矩阵. 若倒过来的数组和原数组一样形式输出提示. 最后输出以字符c替换的字符数组. #include <bits/stdc++.h> ...

  4. 10.1综合强化刷题 Day3 afternoon

    竞赛时间:????年??月??日??:??-??:?? 题目名称 a b c 名称 a b c 输入 a.in b.in c.in 输出 a.out b.out c.out 每个测试点时限 1s 1s ...

  5. EOJ Monthly 2018.7

    准备继续大学acm啦 又要开始愉快的码码码啦 第一次在华东师大OJ上面做题 看来EOJ上的积分体质是假的,我怎么一把上红??? A.数三角形 神tm的防AK题放在A,出题人很不友好啊... 先写了个暴 ...

  6. 基于WPF系统框架设计(1)-为什么要仿Office2010 Ribbon?

    为什么系统框架设计使用Ribbon导航模式? 这得从Office软件的演变说起.微软为什么最后选择使用Ribbon,也许就是很多系统设计要使用Ribbon做功能导航的原因. 你是否还记得曾经使用过的M ...

  7. Blocks的申明调用与Queue当做锁的用法

    Blocks的申明与调用 话说Blocks在方法内使用还是挺方便的,之前都是把相同的代码封装成外部函数,然后在一个方法里需要的时候调用,这样挺麻烦的.使用Blocks之后,我们可以把相同代码在这个方法 ...

  8. MyEclipse Html自动提示功能的设置方法

    操作方法: windows------preferences----------搜索 html----------找到 html Source这项--------- 右边视图,找到 content a ...

  9. linux 学习解决归档管理器打开rar和zip中文文件名乱码问题

    在ubunut下打开windows下压缩的rar文件和zip压缩文件出现中文文件名乱码的问题真的很头疼.文件名乱码其实也没有什么关系是不?至少重命名再改回来或者是使用英文命名都可以克服.不巧的是,如此 ...

  10. IO流中文件和文件夹的删除程序举例

    /* * 删除功能(无论是文件夹还是文件都是用delete方法,仅仅能一级一级的删除.):public boolean delete() * * 注意: * A:假设你创建文件或者目录忘了写盘符路径, ...