123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- //
- // OpenFileViewController.m
- // MusicGradeExam
- //
- // Created by Kyle on 2020/7/21.
- // Copyright © 2020 DayaMusic. All rights reserved.
- //
- #import "OpenFileViewController.h"
- #import <QuickLook/QuickLook.h>
- @interface OpenFileViewController ()<QLPreviewControllerDataSource, QLPreviewControllerDelegate>
- @property (nonatomic, strong) QLPreviewController *previewCtrl;
- @property (nonatomic, strong) NSURL *fileURL;
- @end
- @implementation OpenFileViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self allocTitle:@"曲目详情"];
- [self addChildViewController:self.previewCtrl];
- [self.view addSubview:self.previewCtrl.view];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self requestFileSource];
- }
- - (void)requestFileSource {
- if ([self isFileExist:self.urlString]) { // 本地加载
- self.fileURL = [self getSavePath:self.urlString];
- //刷新界面,如果不刷新的话,不重新走一遍代理方法,返回的url还是上一次的url
- [self.previewCtrl refreshCurrentPreviewItem];
- }
- else { // 网络下载
- [KSRequestManager downloadFileRequestWithFileUrl:self.urlString progress:^(int64_t bytesRead, int64_t totalBytes) {
-
- } success:^(NSURL * _Nonnull fileUrl) {
- self.fileURL = fileUrl;
- [self.previewCtrl refreshCurrentPreviewItem];
- } faliure:^(NSError * _Nonnull error) {
-
- }];
- }
- }
- #pragma mark ------ QLPreviewControllerDataSource
- - (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
- return self.fileURL;
- }
- - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController {
- return 1;
- }
- - (QLPreviewController *)previewCtrl {
- if (!_previewCtrl) {
- _previewCtrl = [[QLPreviewController alloc] init];
- _previewCtrl.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNaviBarHeight - iPhoneXSafeBottomMargin);
- _previewCtrl.delegate = self;
- _previewCtrl.dataSource = self;
- _previewCtrl.currentPreviewItemIndex = 0;
- _previewCtrl.navigationController.navigationBar.userInteractionEnabled = NO;
- }
- return _previewCtrl;
- }
- #pragma mark ----- 文件处理
- - (BOOL)saveFileWithFileName:(NSString *)fileName {
- NSURL *preUrl = [self getSavePath:fileName];
- NSString *copyPath = preUrl.path;
- NSFileManager *fileMamager = [NSFileManager defaultManager];
- NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"FileLibrary"];
- path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
- NSError *error = nil;
- if (![fileMamager copyItemAtPath:copyPath toPath:path error:&error]) {
- [fileMamager removeItemAtPath:copyPath error:nil];
- NSLog(@"拷贝失败:%@", error.userInfo);
- return NO;
- }
- else {
- [fileMamager removeItemAtPath:copyPath error:nil];
- return YES;
- }
- }
- - (NSURL *)getSavePath:(NSString *)fileName {
- // 在Documents目录下创建一个名为FileData的文件夹
- NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"FileLibrary"];
- NSLog(@"%@",path);
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- BOOL isDir = FALSE;
- BOOL isDirExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
- if(!(isDirExist && isDir)) {
- BOOL bCreateDir = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
- if(!bCreateDir){
- NSLog(@"创建文件夹失败!");
- }
- NSLog(@"创建文件夹成功,文件路径%@",path);
- }
-
- path = [path stringByAppendingPathComponent:fileName];
- NSLog(@"file path:%@",path);
- NSURL *url=[NSURL fileURLWithPath:path];
- return url;
- }
- //判断文件是否已经在沙盒中存在
- -(BOOL)isFileExist:(NSString *)fileName {
- NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"FileLibrary"];
- NSString *filePath = [path stringByAppendingPathComponent:fileName];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- BOOL result = [fileManager fileExistsAtPath:filePath];
- return result;
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|