zoukankan      html  css  js  c++  java
  • iOS文档预览功能教程

     本文转载至 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.    
    3. @interface TestViewController : UITableViewController <QLPreviewControllerDataSource>  
    4. {  
    5.   NSArray *arrayOfDocuments;  
    6. }  
    7.    
    8. @end  

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

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

    所要的资源文件可以源码中找到。
  • 相关阅读:
    微信小程序登入实现
    CacheLab实验--深入了解计算机系统实验
    power designer的物理数据模型生成数据字典。
    PowerDesigner15在生成SQL时报错Generation aborted due to errors detected during the verification of the mo
    Mac系统下,如何申请并安装教育版Navicat
    Mac下修改Mysql密码
    数组
    AbstractList源码阅读
    List源码阅读笔记
    AbstractCollection源码阅读笔记
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/4121340.html
Copyright © 2011-2022 走看看