需要的头⽂文件
#import
@import Quartz
;
相关的代理理
QLPreviewPanelDataSource
QLPreviewPanelDelegate
声明变量量
@property (strong) QLPreviewPanel *previewPanel;@property (strong) PreviewItem *previewItem;
PreviewItem写发可以参考上⾯面的MyPreviewItem
调出预览窗⼝口:
- (void)togglePreviewPanel
{
if ([QLPreviewPanel sharedPreviewPanelExists] && [[QLPreviewPanel
sharedPreviewPanel] isVisible]){
[[QLPreviewPanel sharedPreviewPanel] orderOut:nil];}
else
{
[[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil];}
}
QLPreviewPanel实例例只有⼀一个共享,所以在创建前先判断是否存在。弹出预览窗⼝口后会⾃自动调⽤用这⼏几个回调:
#pragma mark - Quick Look panel support
- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel
{
return YES
;
}
- (void)beginPreviewPanelControl:(QLPreviewPanel *)panel
{
// This document is now responsible of the preview panel
// It is allowed to set the delegate, data source and refresh panel.//
_previewPanel = panel
;
panel.delegate = self;
panel.dataSource = self;
}
- (void)endPreviewPanelControl:(QLPreviewPanel *)panel
{
// This document loses its responsisibility on the preview panel
// Until the next call to -beginPreviewPanelControl: it must not
// change the panel's delegate, data source or refresh it.
//
_previewPanel = nil;}
QLPreviewPanel通过QLPreviewPanelDataSource代理理获取⽂文件信息#pragma mark - QLPreviewPanelDataSource
-
(NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel
{
return 1;}
- (id <QLPreviewItem>)previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index
{
return _previewItem.fileURL;}
_previewItem初始化
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString* path = paths[0];
NSString* filePath = [path stringByAppendingPathComponent:@"1.doc"];NSURL* urlFile = [NSURL fileURLWithPath:filePath];
_previewItem = [[PreviewItem alloc] init];_previewItem.fileURL = urlFile;
}