最近在項目中要做一個文檔預覽的功能,做的時候用到了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地址