在视图中显示快速查看预览
我试图在视图中而不是在面板中显示文件的预览。我找到的所有示例都是关于 QLPreviewPanel 的。 :(
预先感谢您的帮助。
I am trying to show the preview of a file in a View instead of in a panel. All examples I found are about QLPreviewPanel
. :(
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 OS X 10.7 开始,有一个用于此目的的公共 API:QLPreviewView.h,它是 QuickLookUI 框架的一部分(它是 Quartz 框架的一部分)。似乎没有关于它的文档,但头文件提供了一些基本信息。
Starting in OS X 10.7, there is a public API for this: QLPreviewView.h, which is part of the QuickLookUI framework (which is part of the Quartz framework). There doesn't seem to be documentation on it, but the header file provides some basic info.
看起来Apple真的希望您使用
QLPreviewPanel
;我看到的唯一可能性是通过将面板设置为图层支持并获取正确子图层的内容来“抓取”预览。像这样的东西(虽然我还没有让它工作):无论如何都不是最终的解决方案(这不能按原样工作),但也许它会为您指明一个有用的方向。
更新:刚刚在
NSImage
上偶然发现了一个类别,由一位名叫 Matt Gemmell 的人编写,它使用QLThumbnailImageCreate
。在他的源代码页上查找“NSImage+QuickLook”。他似乎暗示 QuickLook 面板实际上使用了 QLThumbnailImageCreate。我认为该功能可能是最好的方法。该类别可能会让您的生活更轻松一些。It seems like Apple really wants you to use the
QLPreviewPanel
; the only possibility I see is "scraping" the preview, by setting the panel to layer-backed and getting the contents of the correct sublayer. Something like this (although I haven't gotten it to work):Not a final solution by any means (this doesn't work as is) but maybe it'll point you in a useful direction.
UPDATE: Just stumbled across a category on
NSImage
, written by a fellow named Matt Gemmell, which usesQLThumbnailImageCreate
. Look for "NSImage+QuickLook" on his source code page. He seems to imply that the QuickLook Panel actually usesQLThumbnailImageCreate
. I think that function may be the best way to go. The category might make your life a little easier.我在这方面已经取得了一些进展。子层的内容为零。但老式的子视图可以工作:
当我将 -reloadData 发送到 QLPreviewPane 时,它甚至似乎会更新 myWindow。我感觉粘糊糊的。但不知道下一步该做什么。一个问题是处理子视图的任意大小。我不喜欢 QLPreviewPane 的原因之一是它无法控制窗口大小;它从生成器中获取任意大小的视图并将其显示在屏幕上。我想我可以把它放入滚动视图中。另一个问题是如何处理仍在屏幕上的 QLPreviewPanel。也许将其帧原点设置为屏幕外但我现在需要去做另一项任务。任何进一步的想法将不胜感激。
稍后。我认为这种方法会有问题。首先,我尝试通过发送 setFrameOrigin:NSMakePoint(10000, 10000) 来摆脱 QLPreviewPane 窗口。结果:[QL] 断言失败([事件窗口] == 窗口) - 事件中的窗口错误。然后我尝试省略对 -makeKeyAndOrderFront: 的调用,而是跳到 -reloadData。结果:[QL] QLError():-[QLPreviewPanel reloadData] 在面板没有控制器时调用 - 修复此问题,否则很快就会引发此问题。请参阅 QLPreviewPanel.h 中 -acceptsPreviewPanelControl:/-beginPreviewPanelControl:/-endPreviewPanelControl: 的注释。
第二个错误是可以理解的,但是它表明 QLPreviewPanel 不会尝试查找其数据源,直到它这样做为止。是钥匙或前面订购的。然而,第一个断言表明,除了没有提供适当的 API 来直接获取预览数据之外,也许苹果还为像我这样的休闲黑客设置了一些陷阱。
如果我回到这个问题,下次我会尝试更多 Ken Apeslagh 提出的严重黑客攻击。
I've made some headway with this. The sublayers' contents were nil. But the old-fashioned subviews work:
It even seems to update myWindow when I send -reloadData to the QLPreviewPane. I'm feeling slimy. Not sure what to do next, though. One problem is to deal with the arbitrary size of the subview. One of the reasons why I don't like QLPreviewPane is that there is no control over the window size; it gets a view from the generator at some arbitrary size and splats it on the screen. I guess I could put it into a scroll view. Another issue is how to deal with the QLPreviewPanel which is still on the screen. Maybe set its frame origin to off-screen But I need to go work on another task right now. Any further ideas would be appreciated.
Later. I think this approach is going to be to problematic. First, I tried to get rid of the QLPreviewPane window by sending it a setFrameOrigin:NSMakePoint(10000, 10000). Result: [QL] Assertion failure ([event window] == window) - Wrong window in event. Then I tried to omit the call to -makeKeyAndOrderFront: and instead skip up to -reloadData. Result: [QL] QLError(): -[QLPreviewPanel reloadData] called while the panel has no controller - Fix this or this will raise soon. See comments in QLPreviewPanel.h for -acceptsPreviewPanelControl:/-beginPreviewPanelControl:/-endPreviewPanelControl:.
The second error is understandable, however it indicates that the QLPreviewPanel will not try to find its data source, as it does, until it is made key or ordered front. The first assertion, however, indicates that, besides not providing a proper API to get the preview data directly, maybe Apple has laid some traps for the casual hacker like me.
If I come back to this, next time I'll try the more serious hack proposed by Ken Apeslagh.