1. 程式人生 > >iOS 使用QLPreviewController預覽本地和網路檔案

iOS 使用QLPreviewController預覽本地和網路檔案

最近在專案中要做一個文件預覽的功能,做的時候用到了iOS原生的QLPreviewController類,在此做個記錄分享

首先引入標頭檔案

#import <QuickLook/QuickLook.h>
 
 

遵循代理

QLPreviewControllerDataSource

宣告一個QLPreviewController變數
@property (strong, nonatomic)QLPreviewController *previewController;

再宣告一個NSUrl物件存放要返回的檔案路徑

@property (copy, nonatomic)NSURL *fileURL;

在viewDidLoad中初始化

- (void)viewDidLoad {
    [super viewDidLoad];
    self.previewController  =  [[QLPreviewController alloc]  init];
    self.previewController.dataSource  = self;
}


在預覽本地檔案的事件裡面寫下如下程式碼

//獲取本地檔案路徑
    self.fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"李碧華佳句賞析.doc" ofType:nil]];
    [self presentViewController:self.previewController animated:YES completion:nil];


遵循代理方法

#pragma mark - QLPreviewControllerDataSource
-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    return self.fileURL;
}

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController{
    return 1;
}

這樣本地檔案的預覽就成了

新增檔案到專案的時候要注意,將檔案放到工程專案之後,還要到Build Phases的Copy Bundle Resources中將檔案新增到資源庫,不然使用的時候會找不到檔案而崩潰


接下來是預覽網路檔案

在網路檔案預覽的事件裡面寫下如下程式碼(需要引入af)


NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    NSString *urlStr = @"https://www.tutorialspoint.com/ios/ios_tutorial.pdf";
    NSString *fileName = [urlStr lastPathComponent]; //獲取檔名稱
    NSURL *URL = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    //判斷是否存在
    if([self isFileExist:fileName]) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
        self.fileURL = url;
        [self presentViewController:self.previewController animated:YES completion:nil];
    }else {
        [SVProgressHUD showWithStatus:@"下載中"];
        NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress){
            
        } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
            NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
            NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
            return url;
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            [SVProgressHUD dismiss];
            self.fileURL = filePath;
            [self presentViewController:self.previewController animated:YES completion:nil];
        }];
        [downloadTask resume];
    }

新增檔案是否存在判斷方法

//判斷檔案是否已經在沙盒中存在
-(BOOL) isFileExist:(NSString *)fileName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL result = [fileManager fileExistsAtPath:filePath];
    return result;
}


這樣網路檔案預覽就成了

附上demo地址