views:

331

answers:

1

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  A: 

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;
}
Ken Aspeslagh
You also might want to check this out from Matt Gemmell:http://mattgemmell.com/2007/10/29/how-to-get-a-quick-look-preview-as-an-nsimageIt uses QLThumbnailImageCreate to create a preview (even large). If I remember correctly, the problem I had with was that it only worked with the first page documents.
Ken Aspeslagh
Despite the title of the code from Matt Gemmell, it in fact only returns the thumbnail, not the preview.
Niko Matsakis
Thanks for the private APIs... those look useful. I figure that the only plausible way to do this is to somehow find the generator bundle for a given file and then play the role of the QL panel, accepting HTML, images, etc as needed. Seems doable with enough hacking but not trivial nor particularly robust if Apple makes changes.
Niko Matsakis
If you use the APIs I listed, you shouldn't need to use the generators directly. Let me know if you have any luck.
Ken Aspeslagh