14
votes

I am looking for a way (public or otherwise) to get an NSView, NSImage, CGImageRef, etc that shows the QuickLook preview for a file. Essentially the equivalent of QLThumbnailImageCreate() but for the preview.

The public APIs I can find do not support this. They allow the creation of a thumbnail image or a QLPreviewPanel. The panel does in fact display the quick look preview, but I cannot get access to the preview's appearance to embed it in other views, nor can I display multiple previews at once.

For background, I am writing an app where users can embed links to other files that should be displayed inline, kind of like an <img> tag in HTML. For images like JPGs and PDFs it's easy to figure out what to display. I thought that for other formats I would use Quick Look to generate a nice visual representation of the file's contents. This way the set of formats supported by my application would be easily extensible (just download new Quick Look generators).

1

1 Answers

5
votes

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