传递图像时,QLPreviewRequestSetDataRepresentation似乎不起作用

时间:2018-03-05 13:10:22

标签: macos quicklook

我正在为MacOS上的自定义文档格式制作一个quicklook插件。我们将预览作为jpg数据存储在文档中。该文档是二进制文件而不是捆绑包。到目前为止,通过使用QLThumbnailRequestSetImageWithData将jpg传递给quicklook,一切都可以在quicklook上查看缩略图。

但是,尝试使用QLPreviewRequestSetDataRepresentation对quicklook预览执行完全相同的操作不起作用。所以我甚至试图首先将数据转换为NSImage并传递其tiff表示但是有用。

它似乎尝试创建预览(就像在日志中看到的那样),但是,在没有抱怨的情况下,还可以创建缩略图并仅查看QuickLook预览面板中的缩略图。查看对qlmanage的一次调用的日志,以显示测试文档的预览:

$qlmanage -p Domino.abd
Testing Quick Look preview with files:
    Domino.abd
2018-03-05 12:17:09.415 qlmanage[7268:97108] Creating a preview for file:///Users/Shared/Test/Domino.abd
2018-03-05 12:17:09.419 qlmanage[7268:97108] Size {600, 845.25}
2018-03-05 12:17:09.431 qlmanage[7268:97108] key: Width, value: 600
2018-03-05 12:17:09.431 qlmanage[7268:97108] key: Height, value: 845
2018-03-05 12:17:09.431 qlmanage[7268:97108] key: kCGImageSourceTypeIdentifierHint, value: public.tff
2018-03-05 12:17:09.477 qlmanage[7268:97107] Creating a thumbnail for file:///Users/Shared/Test/Domino.abd

GeneratePreviewForURL中的相关代码如下所示:

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

    NSLog(@"Creating a preview for %@", url);

    // Load the info header of our document
    CInfoHeader *infoHeader = [[CInfoHeader alloc] initWithUrl:url];
    if (![infoHeader load])
    {
        NSLog(@"Failed to load the Info Header");
        return noErr;
    }

    // Cancel the request if necessary
    if (QLPreviewRequestIsCancelled(preview))
        return noErr;

    // Get the thumbnail stored in the Worksheet Crafter document
    NSData *jpgData = [infoHeader imageData];
    if (!jpgData)
    {
        NSLog(@"No image data in the Info Header");
        return noErr;
    }

    // Convert the raw image to an NSImage since passing the raw data did not work even though it should have
    NSImage *image = [[NSImage alloc] initWithData:jpgData];
#if DEBUG
    NSLog(@"Size %@", NSStringFromSize([image size]));
#endif

    // Get the TIFF representation
    CFDataRef imageData = (__bridge CFDataRef)[image TIFFRepresentation];

    // Set up the hints for the preview
    NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt:image.size.width],kQLPreviewPropertyWidthKey,
                                [NSNumber numberWithInt:image.size.height],kQLPreviewPropertyHeightKey,
                                @"public.tff", kCGImageSourceTypeIdentifierHint,
                                nil];
#if DEBUG
    for (id key in properties)
        NSLog(@"key: %@, value: %@", key, [properties objectForKey:key]);
#endif

    // Return the preview to Quick Look
    QLPreviewRequestSetDataRepresentation(preview,  imageData, kUTTypeImage, (__bridge CFDictionaryRef)properties);

    // Report success
    return noErr;
}

为了完整性,可以使用适用于GenerateThumbnailForURL缩略图的相关代码:

OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize)
{
    NSLog(@"Creating a thumbnail for %@", url);

    // Load the info header of our document
    CInfoHeader *infoHeader = [[CInfoHeader alloc] initWithUrl:url];
    if (![infoHeader load])
    {
        NSLog(@"Failed to load the Info Header");
        return noErr;
    }

    // Cancel the request if necessary
    if (QLThumbnailRequestIsCancelled(thumbnail))
        return noErr;

    // Get the thumbnail stored in our document
    NSData *jpgData = [infoHeader imageData];
    if (!jpgData)
    {
        NSLog(@"No image data in the Info Header");
        return noErr;
    }

    // Set the hint to be a jpeg image
    NSDictionary *properties = [NSDictionary dictionaryWithObject:@"public.jpeg" forKey:(__bridge NSString *)kCGImageSourceTypeIdentifierHint];

    // Return the thumbnail to Quick Look
    QLThumbnailRequestSetImageWithData(thumbnail, (__bridge CFDataRef)jpgData, (__bridge CFDictionaryRef)properties);

    // Report success
    return noErr;
}

0 个答案:

没有答案