项目知识点
- Bundle包的制作与使用
- PDF等文件缓存在沙盒(cache)
- UIDocumentInteractionController本地查看及第三方分享
-
QLPreviewController本地预览及网络预预览
Demo地址
注:UIDocumentInteractionController和QLPreviewController都能实现文件的本地查看,笔者在立题的时候对QLPreviewController的网络预览有误解,其实两者本质都是将文件缓存到本地进行查看
区别:
1.UIDocumentInteractionController一次只能浏览一个文件,QLPreviewController可以一起浏览多个文件;2.UIDocumentInteractionController有第三方分享和特定操作,QLPreviewController则没有
QLPreviewController本地预览
1.导入QuickLook
#import <QuickLook/QuickLook.h>
2.定义QLPreviewController全局变量且引用数据源代理
@interface FileReviewController () <QLPreviewControllerDataSource>
@property (nonatomic, strong) QLPreviewController *QLController;
@property (nonatomic, copy) NSURL *fileURL;
@end
self.QLController = [[QLPreviewController alloc] init];
self.QLController.dataSource = self;
3.实现QLPreviewController数据源代理
/// 文件路径
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
return self.fileURL;
}
/// 文件数
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
4.点击事件
- (void)actionLocal {
self.fileURL = [[NSBundle mainBundle] URLForResource:@"Jobs" withExtension:@"pdf" subdirectory:@"PDF.bundle"];
[self presentViewController:self.QLController animated:YES completion:nil];
}
QLPreviewController网络预览
本质是缓存资源到本地然后查看
1.导入AFNetworking
2.点击事件
- (void)actionQLView {
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.QLController animated:YES completion:nil];
//刷新界面,如果不刷新的话,不重新走一遍代理方法,返回的url还是上一次的url
[self.QLController refreshCurrentPreviewItem];
} else {
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) {
self.fileURL = filePath;
[self presentViewController:self.QLController animated:YES completion:nil];
//刷新界面,如果不刷新的话,不重新走一遍代理方法,返回的url还是上一次的url
[self.QLController refreshCurrentPreviewItem];
}];
[downloadTask resume];
}
}
/**
判断文件是否已经在沙盒中存在
@param fileName 文件名
@return 1:存在 0:不存在
*/
- (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;
}
QLPreviewController预览多个文件
只需要改变QLPreviewControllerDataSource
,使用方法跟UITableviewDataSource类似
#pragma mark - QLPreviewControllerDataSource
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
if (index == 0) {
return [[NSBundle mainBundle] URLForResource:@"image" withExtension:@"png" subdirectory:@"PDF.bundle"];
} else {
return self.fileURL;
}
}
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 2;
}
展示页可以左右滑动切换文件预览,也可以点击右下方按钮跳转到文件选择页