Mac Quick Look Preview in an NSView or NSImage?

匆匆过客 提交于 2019-11-30 08:31:24

I looked into this extensively once a while back and was not able to find an easy way to do it. Depending on the type of file, QuickLook generates different kinds of output. For example, for iWork files, the generator makes HTML that it displays in a WebView. For other types it returns different types of data.

I never ended up using the code, but here's some code I dug up and some private APIs that might be handy:

id QLPreviewCreate(CFAllocatorRef allocator, CFURLRef url,  CFDictionaryRef options);
id QLPreviewCopyBitmapImage(id preview);
id QLPreviewCopyData(id preview);
NSString* QLPreviewGetPreviewType(id preview);
id QLPreviewCopyProperties(id preview);

- (NSData *)getDataForFile:(NSString *)path
{

    NSURL *fileURL = [NSURL fileURLWithPath:path];

    id preview = QLPreviewCreate(kCFAllocatorDefault, fileURL, 0);

    if (preview)
    {
        NSString* previewType = QLPreviewGetPreviewType(preview);

        if ([previewType isEqualToString:@"public.webcontent"])
        {
            // this preview is HTML data
            return QLPreviewCopyData(preview);
        }
        else
        {
           NSLog(@"this type is: %@", previewType);
           // do something else
        }

    }

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