码迷,mamicode.com
首页 > 其他好文 > 详细

文件缓存预览查看

时间:2021-04-22 16:01:27      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:contents   adt   elf   ber   rgb   content   ini   nsfile   mat   

#import <QuickLook/QuickLook.h>

@interface VC()<QLPreviewControllerDataSource,QLPreviewControllerDelegate>

@property (nonatomic,strong)NSURL *fileURL; //文件预览路径

-(void)showFilewithUrlString:(NSString*)string{
    NSURL *targetURL = [NSURL URLWithString:string];
    NSString *docPath = [self documentsDirectoryPath];
    NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docPath, [targetURL lastPathComponent]];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL hasDownLoad= [fileManager fileExistsAtPath:pathToDownloadTo];
    if (!hasDownLoad) {
        NSData *fileData = [[NSData alloc] initWithContentsOfURL:targetURL];
        // Get the path to the App‘s Documents directory
//        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
//        NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docPath, [targetURL lastPathComponent]];
        BOOL bol = [fileData writeToFile:pathToDownloadTo atomically:YES];
        NSLog(@"文件缓存:%@ \n结果:%d",pathToDownloadTo,bol);
        self.fileURL = [NSURL fileURLWithPath:pathToDownloadTo];
    }
    QLPreviewController *qlVC = [QLPreviewController new];
    qlVC.delegate = self;
    qlVC.dataSource = self;
    [self presentViewController:qlVC animated:YES completion:nil];
}

- (NSString *)documentsDirectoryPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectoryPath = [paths objectAtIndex:0];
    return documentsDirectoryPath;
}

#pragma mark  QLPreviewControllerDelegate
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}

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

 

文件缓存预览查看

标签:contents   adt   elf   ber   rgb   content   ini   nsfile   mat   

原文地址:https://www.cnblogs.com/frounk/p/14686421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!