iOS 使用QLPreviewController預覽本地和網路檔案

來源:互聯網
上載者:User

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








相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.