iOS快速预览——QuickLook

作者: Snoopy008 | 来源:发表于2016-09-30 10:09 被阅读304次

好久没碰iOS了, 前几天回顾了一下之前所做的项目,发现有一些新奇的东西。今天我问来讲讲QuickLook。这个功能用的比较少,但是如果需要用它的地方那一定是最简洁的实现形式。

Snip20160930_4.png

我们先来看看效果

音乐.png 图片.png Excel表格.png

看看是如何实现的,此处我是将它与UITableView结合使用的。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self quickLookFileAtIndex:indexPath.row];
    
}

- (void)quickLookFileAtIndex:(NSInteger)index{
    QLPreviewController *previewController =[[QLPreviewController alloc]init];
    previewController.delegate=self;
    previewController.dataSource=self;
    [previewController setCurrentPreviewItemIndex:index];
    [self presentViewController:previewController animated:YES completion:nil];
}

当点击表格的行就会触发。
但是还要实现QLPreviewController的两个代理:QLPreviewControllerDelegate、QLPreviewControllerDataSource

//QuickLook文件数量
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
    return attachmentModels.count;
}


//QuickLook代理
-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
    
    JFAttachmentModel *attachmentModel = attachmentModels[index];
    NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/file", pathDocuments];
    NSString *sandBoxFilePath = [filePath stringByAppendingPathComponent:attachmentModel.fileName];
    return [NSURL fileURLWithPath:sandBoxFilePath];
    
}

我这里将所有的文件都存在了文件沙盒里了,根据文件名去找文件。
是不是很简单,几句简单的代码就可以搞定手机上大部分格式文件的预览。

结语##

QuickLook几乎可以搞定几乎所有的文件,除了图片、音乐,视频、PDF、Word等都是可以。但是其可定制部分比较少,样式比较单一。
欢迎大神指正。

相关文章

网友评论

  • sunney0:你好,请问有gihub demo :grin: 么?
    Snoopy008:@sunney0 这个好像不可以
    sunney0:@Snoopy008 嗯,谢谢,请问我预览一个pdf时,我可以获得这个pdf的页数和每一页的数据么,怎么获得。看你文章里,只传了文档的数量和地址,分页预览时系统自己做的把,有方法获取页数和数据吗? :grin:
    Snoopy008:@sunney0 这个有时间我做个demo出来,其实很容易的

本文标题:iOS快速预览——QuickLook

本文链接:https://www.haomeiwen.com/subject/oomlyttx.html