Mac OS X平台下QuickLook开发教程

一、引言

      Quick Look技术是Apple在Mac OS X 10.5中引入的一种用于快速查看文件内容的技术。用户只需要选中文件单击空格键即可快速查看文件内容,可以在不打开文件的情况下快速浏览内容。公司是做全景视频开发的,具备自己的全景视频文件格式。因此,做一款针对自有视频格式的QuickLook插件显得非常有必要。QuickLook的技术资料非常丰富,不仅官方有着详尽的文档,互联网上也有不少开发者总结的开发经验。即便如此,在开发的过程中也碰到了不少的坑,如今总结在此。最终的QuickLook效果如下所示:

     

二、着手开发

      QuickLook插件是Apple官方推出的一项技术,在XCode中可以直接创建QuickLook Plugins模板工程:

      创建的模板工程中包含三个源文件:GenerateThumbnailForURL.c, GeneratePreviewForURL.c, main.c。其作用可顾名思义,不过我们只要修改前面两个文件就可以了。至于main.c文件,官方是不推荐我们去修改的。GenerateThumbnailForURL.c用于生成缩略图,如下是一种模板实现:

OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize)
{
    @autoreleasepool
    {
        // Get the UTI properties
        NSDictionary* uti_declarations = (__bridge_transfer NSDictionary*)UTTypeCopyDeclaration(contentTypeUTI);
        
        // Get the extensions corresponding to the image UTI, for some UTI there can be more than 1 extension (ex image.jpeg = jpeg, jpg...)
        // If it fails for whatever reason fallback to the filename extension
        id extensions = uti_declarations[(__bridge NSString*)kUTTypeTagSpecificationKey][(__bridge NSString*)kUTTagClassFilenameExtension];
        NSString* extension = ([extensions isKindOfClass:[NSArray class]]) ? extensions[0] : extensions;
        if (nil == extension)
            extension = ([(__bridge NSURL*)url pathExtension] != nil) ? [(__bridge NSURL*)url pathExtension] : @"";
        extension = [extension lowercaseString];
        
        // Create the properties dic
        CFTypeRef keys[1] = {kQLThumbnailPropertyExtensionKey};
        CFTypeRef values[1] = {(__bridge CFStringRef)extension};
        CFDictionaryRef properties = CFDictionaryCreate(kCFAllocatorDefault, (const void**)keys, (const void**)values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
        
        // Check by extension because it's highly unprobable that an UTI for these formats is declared
        // the simplest way to declare one is creating a dummy automator app and adding imported/exported UTI conforming to public.image
        CGImageRef img_ref = NULL;
        CFStringRef filepath = CFURLCopyPath(url);
        if ([extension isEqualToString:@"insp"])
        {
            if (!QLThumbnailRequestIsCancelled(thumbnail))
            {
                // 1. decode the image 
                img_ref = decode_insp_at_path(filepath, NULL);    
                if (filepath != NULL)
                    CFRelease(filepath);
                
                // 2. render it
                if (img_ref != NULL)
                {
                    QLThumbnailRequestSetImage(thumbnail, img_ref, properties);
                    CGImageRelease(img_ref);
                }
                else
                    QLThumbnailRequestSetImageAtURL(thumbnail, url, properties);
            }
            else
                QLThumbnailRequestSetImageAtURL(thumbnail, url, properties);
        }
        else if ([extension isEqualToString:@"insv"])
        {
            if (!QLThumbnailRequestIsCancelled(thumbnail))
            {
                // 1. decode the image
                img_ref = decode_insv_at_path(filepath, NULL);
                if (filepath != NULL)
                    CFRelease(filepath);
                
                // 2. render it
                if (img_ref != NULL)
                {
                    QLThumbnailRequestSetImage(thumbnail, img_ref, properties);
                    CGImageRelease(img_ref);
                }
                else
                    QLThumbnailRequestSetImageAtURL(thumbnail, url, properties);
            }
            else
                QLThumbnailRequestSetImageAtURL(thumbnail, url, properties);
        }
        else
            QLThumbnailRequestSetImageAtURL(thumbnail, url, properties);
        
        if (properties != NULL)
            CFRelease(properties);
    } 
    return noErr;
}

  基本流程为:先获取文件UTI(Uniform Type Identifiers),然后再根据文件的扩展名来过滤文件,继而调用相应的解码库对图片或视频进行解码,构建CGImage引用返回。

      GeneratePreviewForURL.c文件则用于完成预览图的生成。缩略图是就用单击空格键时弹出来的图,基本模板代码如下:

OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
    @autoreleasepool
    {
        NSString* extension = [[(__bridge NSURL*)url pathExtension] lowercaseString];
        image_infos infos;
        memset(&infos, 0, sizeof(image_infos));
        CGImageRef img_ref = NULL;
        CFStringRef filepath = CFURLCopyPath(url);
        if ([extension isEqualToString:@"insp"])
        {
            // 1. decode the image
            if (!QLPreviewRequestIsCancelled(preview))
            {
                img_ref = decode_insp_at_path(filepath, &infos);
                if (filepath != NULL)
                    CFRelease(filepath);
                
                // 2. render it
                CFDictionaryRef properties = create_properties(url, infos.filesize, infos.width, infos.height, true);
                if (img_ref != NULL)
                {
                    // Have to draw the image ourselves
                    CGContextRef ctx = QLPreviewRequestCreateContext(preview, (CGSize){.width = OUT_WIDTH, .height = OUT_HEIGHT+LOGO_HEIGHT}, YES, properties);
                    CGContextDrawImage(ctx, (CGRect){.origin = CGPointZero, .size.width = OUT_WIDTH, .size.height = OUT_HEIGHT+LOGO_HEIGHT}, img_ref);
                    QLPreviewRequestFlushContext(preview, ctx);
                    CGContextRelease(ctx);
                    CGImageRelease(img_ref);
                }
                else
                    QLPreviewRequestSetURLRepresentation(preview, url, contentTypeUTI, properties);
                if (properties != NULL)
                    CFRelease(properties);
            }
        }
        else if([extension isEqualToString:@"insv"])
        {
            // 1. decode the image
            if (!QLPreviewRequestIsCancelled(preview))
            {
                img_ref = decode_insv_at_path(filepath, &infos);
                if (filepath != NULL)
                    CFRelease(filepath);
                
                // 2. render it
                CFDictionaryRef properties = create_properties(url, infos.filesize, infos.width, infos.height, true);
                if (img_ref != NULL)
                {
                    // Have to draw the image ourselves
                    CGContextRef ctx = QLPreviewRequestCreateContext(preview, (CGSize){.width = OUT_WIDTH, .height = OUT_HEIGHT+LOGO_HEIGHT}, YES, properties);
                    CGContextDrawImage(ctx, (CGRect){.origin = CGPointZero, .size.width = OUT_WIDTH, .size.height = OUT_HEIGHT+LOGO_HEIGHT}, img_ref);
                    QLPreviewRequestFlushContext(preview, ctx);
                    CGContextRelease(ctx);
                    CGImageRelease(img_ref);
                }
                else
                    QLPreviewRequestSetURLRepresentation(preview, url, contentTypeUTI, properties);
                if (properties != NULL)
                    CFRelease(properties);
            }

        }
        else
        {
            // Standard images (supported by the OS by default)
            size_t width = 0, height = 0, file_size = 0;
            properties_for_file(url, &width, &height, &file_size);
            
            // Request preview with updated titlebar
            CFDictionaryRef properties = create_properties(url, file_size, width, height, false);
            QLPreviewRequestSetURLRepresentation(preview, url, contentTypeUTI, properties);
            
            if (properties != NULL)
                CFRelease(properties);
        }
    }
    return kQLReturnNoError;
}

  代码逻辑甚至更简单,其中的create_properties()函数用于给预览窗口添加宽高、图片名等信息。不需要的话可以去掉。上面的模板代码编写好之后,QuickLook插件的主要工作就是视频和图片的编解码了。编译好的QuickLook插件是一个以qlgenerator为扩展名的Bundle。官方推荐的安装位置有三个:(1)~/Library/QuickLook/:存放第三方开发的QuickLook插件,针对当前用户的,只有当前用户登录了才会加载插件。(2)/Library/QuickLook:存放第三方开发的QuickLook插件,这是针对所有用户起作用的。(3)/System/Library/QuickLook/:这里只存放苹果公司开发的QuickLook插件,所有用户都能用。可以根据自身需要将QuickLook插件安装到相应的位置。

三、注意事项

      (1) 确定并设置文件UTI。QuickLook插件需要根据文件的UTI来关联,因此首要的一步是确定自有文件格式的UTI。那么怎么确定呢?其实有个命令可以查看文件的UTI元信息:mdls。kMDItemContentType即为文件的UTI信息。把获得的UTI添加到QuickLook工程当中的Info.plist文件中去即可。

 

     (2)日志路径。在开发QuickLook插件的过程中,难免会遇到崩溃的情况。这个时候日志是非常重要的一种调试手段。QuickLook插件出现异常的第一步应该去查看/var/log/system.log文件。这个是OSX系统的系统日志文件。QuickLook插件如出现加载异常现象,里面都会有记录。其次我们应该去/Users/[user name]/Library/Logs/DiagnosticReports/目录下去查看崩溃日志。 

      (3) 需要注意的是,在GeneratePreviewForURL()GenerateThumbnailForURL()方法中传进来的文件路径以URL形式存在的。也就是说,当路径中存在中文时,会进行URL Encode编码。也就是说,中文"直播间"会变成"%E7%9B%B4%E6%92%AD%E9%97%B4"。在解析的时候要注意进行URL Decode操作,否则的话无法读取到文件。

    (4)qlmanage的使用。qlmanage可以用于清除QuickLook缓存,也可以用来查看当前系统中存在哪些QuickLook插件,也可以查看哪些文件是和哪些QuickLook插件关联的。这对于判断你的QuickLook插件是否起作用很方便:

      (5) install_name_toolutool。这两个命令配合使用,主要用于修改动态库的链接路径。主要使用方法为:

    • utool -L *.dylib
    • install_name_tool -id "new_path" *.dylib
    • install_name_tool -change "old_path" "new_path" *.dylib   

四、参考链接

  1. http://blog.10to1.be/cocoa/2012/01/27/creating-a-quick-look-plugin/
  2. http://apple.stackexchange.com/questions/153595/how-can-i-see-which-quicklook-plugin-is-responsible-for-which-data-type
  3. https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/Quicklook_Programming_Guide/Introduction/Introduction.html
posted @ 2016-07-27 20:16  24K纯开源  阅读(3517)  评论(1编辑  收藏  举报