Swift 3 Cocoa: use QuickLook to preview file in OS X

一个人想着一个人 提交于 2019-12-07 12:42:00

问题


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.


回答1:


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.

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
    }
}


来源:https://stackoverflow.com/questions/43880352/swift-3-cocoa-use-quicklook-to-preview-file-in-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!