1. 程式人生 > >ios-檔案預覽-QLPreViewController

ios-檔案預覽-QLPreViewController

       最近在做的專案中有相關pdf檔案預覽的功能,所以瞭解了一下相關的資料。基本上網上給出來的方法有兩種,分別是用UIWebVie和CGContextDrawPDFPage,可以點選檢視,但是本文所要講的是另外一種方式-----使用QLPreViewController進行預覽,QLPreViewController不僅支援PDF,還支援其他多種檔案的預覽,可謂功能強大。

         先看下官方文件的說法:

A displayed preview includes a title taken from the last path component of the item URL. You can override this by implementing a 

preview​Item​Title accessor for the preview item.

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

        使用方式:

1.首先新建一個QLPreviewController物件preview;

2.遵循協議QLPreviewControllerDataSource,設定代理;

3.實現協議方法:返回文件數量方法- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller;

                           返回URL的方法 - (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index;

4.模態或push出QLPreviewController的物件preview。

   關鍵程式碼:

#pragma mark -QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}

- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    
    NSString *filePath = [[HKClassRoomManager sharedInstance] getFilePath:self.classFile];
    
    NSAssert(filePath, @"preview nil item");
    
    NSFileManager* fileManager = [NSFileManager defaultManager];
    NSError* error = nil;
    if ([fileManager fileExistsAtPath:filePath]) {
        NSDictionary* fileInfo = [fileManager attributesOfItemAtPath:filePath
                                                               error:&error];
        NSInteger fileSize = [[fileInfo objectForKey:NSFileSize] integerValue];
        if (fileSize >= 1024 * 1024 * 10) {
            [NSObject showHudTipStr:@"檔案較大,載入可能需要一段時間,請等待...."];
        }
    }
    
    NSData *fileData = [NSData dataWithContentsOfFile:filePath];
    
    //判斷是UNICODE編碼
    NSString *isUNICODE = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
    
    //還是ANSI編碼
    NSString *isANSI = [[NSString alloc] initWithData:fileData encoding:-2147482062];
    
    if (isUNICODE) {
        
        NSString *retStr = [[NSString alloc]initWithCString:[isUNICODE UTF8String] encoding:NSUTF8StringEncoding];
        NSData *data = [retStr dataUsingEncoding:NSUTF16StringEncoding];
        NSError *error = nil;
        [data writeToFile:filePath options:NSDataWritingAtomic error:&error];
    }
    else if(isANSI){
        
        NSData *data = [isANSI dataUsingEncoding:NSUTF16StringEncoding];
        NSError *error = nil;
        [data writeToFile:filePath options:NSDataWritingAtomic error:&error];
    }
    
    
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    return fileURL;
}