Swift 3 Cocoa:使用QuickLook在OS X中预览文件 [英] Swift 3 Cocoa: use QuickLook to preview file in OS X

查看:207
本文介绍了Swift 3 Cocoa:使用QuickLook在OS X中预览文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode 8.3.2我在命令列表中找不到QLPreviewPanel,也不知道如何做(必须使用哪个命令)以在ViewController中显示文件预览.

Xcode 8.3.2 I don't find QLPreviewPanel in the command list and I don't know how to do (which command must be used) to display a file preview in a ViewController.

推荐答案

首先,您需要将import Quartz语句添加到您的NSViewCOntroller中.第二步是将QLPreviewPanelDataSource, QLPreviewPanelDelegate添加到其声明中.接下来,您只需要获取共享的 QLPreviewPanel 的引用,使视图控制器为其dataSourcedelegate并使其窗口键和命令位于前面.

First of all you will need to add the import Quartz statement to your NSViewCOntroller. Second step is to add QLPreviewPanelDataSource, QLPreviewPanelDelegate to its declaration. Next you just need to get a reference of the shared QLPreviewPanel, make the view controller its dataSource and delegate and make its window key and order front.

您还需要将numberOfPreviewItems和PreviewItemAt方法添加到控制器.您可以按照以下步骤进行操作:

You will need also to add numberOfPreviewItems and previewItemAt methods to your controller. You can do it as follow:

import Quartz

class ViewController: NSViewController,  QLPreviewPanelDataSource, QLPreviewPanelDelegate {

    @IBAction func button(_ sender: NSButton) {
        if let sharedPanel = QLPreviewPanel.shared() {
            sharedPanel.delegate = self
            sharedPanel.dataSource = self
            sharedPanel.makeKeyAndOrderFront(self)
        }
    }

    func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
        return 1
    }

    func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
        let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("yourImageAtTheDocs.png")
        return url as QLPreviewItem
    }
}

这篇关于Swift 3 Cocoa:使用QuickLook在OS X中预览文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆