Why does QLPreviewRequestSetDataRepresentation on

2019-01-28 03:30发布

问题:

My quick look generator used to work properly but is now broken.
Is it a bug or am I doing something wrong?

Here’s my code:

OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, 
                               CFURLRef url, CFStringRef contentTypeUTI, 
                               CFDictionaryRef options) {

    NSDictionary * myDoc = [NSDictionary dictionaryWithContentsOfURL:(NSURL *)url];

        if (myDoc) {

            NSData * pngData = [myDoc valueForKey:@"pngPreview"];

            if (pngData) {

                QLPreviewRequestSetDataRepresentation(preview,(__bridge CFDataRef)pngData,
                                                      kUTTypeImage,NULL);
            }
        }
}

My doc is a normal plist with a png preview stored as data in it.
I checked that pngPreview does contain png data, I created the image and its size was 350×350.

However, I’m constantly getting these errors:

qlmanage[702] : CGImageCreate: invalid image size: 0 x 0.
qlmanage[702:303] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x9e27, name = 'com.apple.tsm.portname' See /usr/include/servers/bootstrap_defs.h for the error codes.
qlmanage[702:303] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x3f2b, name = 'com.apple.CFPasteboardClient' See /usr/include/servers/bootstrap_defs.h for the error codes.
qlmanage[702:303] Failed to allocate communication port for com.apple.CFPasteboardClient; this is likely due to sandbox restrictions

My app is not sandboxed so I don’t think the last 3 errors are important.

I used to use kUTTypePNG but have tried kUTTypeImage to no avail (the docs for QLPreviewRequestSetDataRepresentation says currently supported UTIs are kUTTypeImage, kUTTypePDF, kUTTypeHTML, kUTTypeXML, kUTTypePlainText, kUTTypeRTF, kUTTypeMovie, and kUTTypeAudio).

Other points to consider: The docs state: "The binary of a Quick Look generator must be universal and must be 32-bit only." This page But this page states: "For OS X v10.6 and later, you must build Quick Look generators for both 32- and 64-bit." Which is rather unclear...
How do I set my target?

回答1:

Facing the same problem I've decided to go an alternate route: use QLPreviewRequestCreateContext to get a context in which to draw my image in:

QLPreviewRequestRef preview; // The preview request passed to GeneratePreviewForURL()
CGImageRef image;  // Create your CGImage however you like
CGSize size = CGSizeMake(CGImageGetWidth(image), CGImageGetHeight(image));
CGContextRef ctxt = QLPreviewRequestCreateContext(preview, size, YES, nil);
CGContextDrawImage(ctxt, CGRectMake(0, 0, size.width, size.height), image);
QLPreviewRequestFlushContext(preview, ctxt);
CGContextRelease(ctxt);

At least that works...