相关的头⽂文件
#import <QuickLook/QuickLook.h>
代理理
QLPreviewControllerDataSource
声明⼀一个QLPreviewController变量量
@property (nonatomic,copy) NSURL *urlFile;
@property (nonatomic,strong) QLPreviewController *previewController;
在viewDidLoad中初始化- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_previewController = [[QLPreviewController alloc] init];
_previewController.dataSource = self;
NSString* pathRes = [[NSBundle mainBundle] pathForResource:@"1.doc" ofType:nil];
self.urlFile = [NSURL fileURLWithPath:pathRes];}
代理理
#pragma mark - QLPreviewControllerDataSource
-
(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController
*)controller
{
return 1;}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller
previewItemAtIndex:(NSInteger)index
{
return self.urlFile;// or return self.previewItem;}
这⾥里里可以直接返回NSURL,也可以返回QLPreviewItem对象,QLPreviewItem是协议,类似c++的虚类
MyPreviewItem* previewItem = [[MyPreviewItem alloc] init];previewItem.previewItemTitle = @"测试";previewItem.previewItemURL = self.urlFile;
return previewItem;
MyPreviewItem实现QLPreviewItem:
@interface MyPreviewItem : NSObject<QLPreviewItem>
@property(nullable, nonatomic) NSURL * previewItemURL;@property(nullable, nonatomic) NSString * previewItemTitle;@end
@implementation MyPreviewItem
@end
如果要实现⽹网络⽂文件预览,多⼀一步从⽹网络下载⽂文件到本地,再把本地⽂文件地址给urlFile。
注意⼀一点,从NSString到NSURL要⽤用NSURL::fileURLWithPath,⽽而不不是NSURL::URLWithString,前者应该是在路路径前多加⼀一个file://