Showing posts with label QuickLook. Show all posts
Showing posts with label QuickLook. Show all posts

Tuesday, July 17, 2012

Tutorial: How to use QLPreviewController to preview files.


This tutorial will talk about implementation of preview feature of documents, images and files available on iOS 4.0 onwards. Apple reference document says:


A Quick Look preview controller can display previews for the following items:
  • iWork documents
  • Microsoft Office documents (Office ‘97 and newer)
  • Rich Text Format (RTF) documents
  • PDF files
  • Images
  • Text files whose uniform type identifier (UTI) conforms to the public.text type (see Uniform Type Identifiers Reference)
  • Comma-separated value (csv) files


You need to add QuickLook.framework in your project. and import QuickLook/QuickLook.h
and add protocols <QLPreviewControllerDataSource,QLPreviewControllerDelegate>





QLPreviewController *previewController=[[QLPreviewController alloc]init];
previewController.delegate=self;
previewController.dataSource=self;
[self presentModalViewController:previewController animated:YES];
[previewController.navigationItem setRightBarButtonItem:nil];


You need to add datasource methods for sure. Return more than 1 for numberOfPreviewItemsInPreviewController if you want user to be able to preview next and previous file while in preview mode. Else return 1 if you want user to exclusively preview only selected file.
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index


You may add delegate methods  optionally depending on your requirements. In Sample code, I have used these delegate methods to add the zooming effect where the preview seems to come out of the same button clicked by user.



- (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id <QLPreviewItem>)item
- (CGRect)previewController:(QLPreviewController *)controller frameForPreviewItem:(id <QLPreviewItem>)item inSourceView:(UIView **)view



For more information, you can view the Apple reference document.

Link of the sample code to download.