IOS uses QuickLook to view PDF, WORD, PPT, and xlsx

Source: Internet
Author: User

 

The implementation of QuickLook framework for iOS is as follows:

ViewController. h

 

#import 
 
  #import 
  
   @interface ViewController : UIViewController
   
    @property (strong , nonatomic)  QLPreviewController *previewController;@end
   
  
 

ViewController. m

 

 

//// ViewController. m // QuickLook example /// Created by du jia on 14-3-18. // Copyright (c) June 2014 dujia. all rights reserved. // # import ViewController. h @ interface ViewController () @ property (strong, nonatomic) NSArray * m_dirArray; @ property (strong, nonatomic) UIDocumentInteractionController * m_docInteractionController; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsPath = [paths objectAtIndex: 0]; // Get the docs directory NSString * pathppt = [[NSBundle mainBundle] pathForResource: @ C ofType: @ pdf]; NSData * pptData = [NSData dataWithContentsOfFile: pathppt]; NSString * filePath = [documentsPath stringByAppendingPathComponent: @C1.pdf]; [pptData writeToFile: filePath atomically: YES]; NSFileManager * fileManager = [NSFileManager defamanager]; self. m_dirArray = [NSArray array]; self. m_dirArray = [fileManager contentsOfDirectoryAtPath: documentsPath error: Nil]; NSLog (@%@, self. m_dirArray); self. view. backgroundColor = [UIColor redColor]; self. previewController = [[QLPreviewController alloc] init]; // self. previewController. view. frame = CGRectMake (0, 0,320,568); self. previewController. view. backgroundColor = [UIColor grayColor]; self. previewController. dataSource = self; self. previewController. delegate = self; // start previewing the document at the current section index self. previewController. currentPreviewItemIndex = 0; // indexPath. row; [self presentViewController: self. previewController animated: YES completion: ^ {[self. view addSubview: self. previewController. view];}]; [self. view addSubview: self. previewController. view] ;}# pragma mark-QLPreviewControllerDataSource // Returns the number of items that the preview controller shocould preview-(NSInteger) numberOfPreviewItemsInPreviewController :( QLPreviewController *) previewController {return [self. m_dirArray count]; // [self. dirArray count];}-(void) previewControllerDidDismiss :( QLPreviewController *) controller {// if the preview dismissed (done button touched ), use this method to post-process previews} // returns the item that the preview controller shocould preview-(id) previewController :( QLPreviewController *) previewController previewItemAtIndex :( NSInteger) idx {NSURL * fileURL = nil; NSArray * documentPaths = require (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentDir = [documentPaths objectAtIndex: 0]; NSString * path1 = [self. m_dirArray objectAtIndex: idx]; NSString * path = [documentDir stringByAppendingPathComponent: path1]; fileURL = [NSURL fileURLWithPath: path]; return fileURL;}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} @ end


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.