iOS文檔預覽功能教程

來源:互聯網
上載者:User

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的標頭檔如下:

#import <QuickLook/QuickLook.h> @interface TestViewController : UITableViewController <QLPreviewControllerDataSource>{  NSArray *arrayOfDocuments;} @end

修改 controller執行檔案如下

#import "TestViewController.h"@implementation TestViewController#pragma mark -#pragma mark Initialization/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/-(id)init{  if (self = [super init])  {arrayOfDocuments = [[NSArray alloc] initWithObjects: @"iOSDevTips.png", @"Remodel.xls", @"Core J2ME Technology.pdf", nil];  }  return self;}/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (void)loadView {[super loadView];[self setTitle:@"Files Available for Preview"];}#pragma mark -#pragma mark Table Management// Customize the number of sections in the table view./*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;}/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [arrayOfDocuments count];}/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  static NSString *CellIdentifier = @"tableRow";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  if (cell == nil)cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    // ???[[cell textLabel] setText:[arrayOfDocuments objectAtIndex:indexPath.row]];[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];return cell;}/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  // When user taps a row, create the preview controllerQLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];// Set data source[previewer setDataSource:self];    // Which item to preview[previewer setCurrentPreviewItemIndex:indexPath.row];// Push new viewcontroller, previewing the document[[self navigationController] pushViewController:previewer animated:YES];}#pragma mark -#pragma mark Preview Controller/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {return [arrayOfDocuments count];}/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {// Break the path into it's components (filename and extension)NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];// Use the filename (index 0) and the extension (index 1) to get path  NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];                             return [NSURL fileURLWithPath:path];}#pragma mark -#pragma mark Cleanup/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (void)dealloc {// Free up all the documents[arrayOfDocuments release];[super dealloc];}@end

修改Appdelegate如下

- (void)applicationDidFinishLaunching:(UIApplication *)application {     // Create and initialize the window  window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];   // Create test view controller  vc = [[TestViewController alloc] init];   // Create navigation controller   nav = [[UINavigationController alloc] initWithRootViewController:vc];   [window addSubview:[nav view]];    [window makeKeyAndVisible];}

所要的資源檔可以源碼中找到。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.