iOS文档预览功能教程

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

文档iosuinavigationcontrollerextensionmicrosoftcomponents

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. }

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

时间: 2024-08-29 06:01:10

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

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

office web apps安装 系统要求为Windows Server 2012, 注意:转换文档需要两台服务器,一台为转换server,另外一台为域控server.(至于为什么要两台,这个请自行google,微软是这样要求的,我也不懂) 公司的系统现在正是使用这个实现文档在线预览的,不久的将来一定会上线.后面的文章我会附上即将上线的文档转换服务的截图. 以前公司使用的再现预览方案是把文档转换成pdf文件,再然后转换成图片,实现预览.但是同步转换的时候总是出现问题,服务器内存消耗严重,apa

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

最近再做项目时,要在手机端实现在线文档预览的功能.于是百度了一下实现方案,大致是将文档转换成pdf,然后在通过插件实现预览.这些方案没有具体实现代码,也没有在线预览的地址,再加上项目时间紧迫.只能考虑其它方案,这时微软的office web apps方案映入眼帘,于是和同事一起用一台PC机折腾了几天终于完成了部署,希望通过本篇记录下安装过程和遇到的坑.目前使用该方案的有 明道 微软,我部署的服务地址:http://myscloud.vicp.cc/op/generate.aspx 下面是在线预览

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

上一篇文章已经介绍了整个安装过程了.只要在浏览器中输入文档转换server的ip,会自动跳转,出现如下页面. 那么就可以实现本地文档预览了,你可以试试.(注意:是本地哦,路径不要写错,类似“\\file\share”.如果不能预览,那么还要注意文档的权限哦,不多说.) 看到这个,就算完成80%以上的工作了,那么现在只需要集成到自己的项目中. 以下也主要是参考:http://www.cnblogs.com/poissonnotes/p/3277280.html 这篇文章也是超级经典.(上篇安装的也

文档预览的设计思考

文档预览服务的功能设计 1. 安全控制 2.浏览器兼容 3.适应不同的终端 4.加载优化 5.易扩展 6.集群部署 7.客户端SDK的开发(Java后端SDK.前端的Web SDK) 已有的一些方案 百度文库 I Doc View

pdf 文档预览

最近在写一个功能, 网站在浏览器上实现文件预览功能, 我前端的处理方式, 可以使用H5 的新标签<embed> 或者<iframe> src 属性给个文件的地址,就可以预览了,当然如果是图片或者pdf 格式的文件, 是可以的,但是doc,ppt 等文档文件必须 要转成pdf 格式的文件.后端用openOffice.org,参考文档:http://www.cnblogs.com/star-studio/archive/2011/12/09/2281807.html:先将文件下载到本地

JAVA+FlexPaper+OpenOffice+SWFTools文档预览

http://blog.csdn.net/core_star/article/details/10148047 1.软件环境: openoffice:启动openoffice服务:soffice.exe -headless -nologo -norestore -accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager swftools 2.所需组件: flexpaper : flexpaper_flash_debu

在线文档预览

word excel powerpoint website

android studio布局提示文档预览,发布自动取消

Java实现office文档与pdf文档的在线预览功能

最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完.查找资料发现我们要实现的过程就是把office转换成pdf,当然pdf就不用转换了.然后在pdf转换为swf文件,在浏览器实现预览swf文件.整个过程就是这样,看起来很简单,实际操作起来会出现各种问题.下面我就把自己写的这一小功能记录下来. 1.首先我们需要找到可以把office转换成pdf的方法