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];
-
遵循代理方法
-
-
-
-(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;
-
-
}
这样网络文件预览就成了