一、引言
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_tool和utool。這兩個命令配合使用,主要用於修改動態庫的鏈接路徑。主要使用方法為:
-
- utool -L *.dylib
- install_name_tool -id "new_path" *.dylib
- install_name_tool -change "old_path" "new_path" *.dylib
四、參考鏈接
- http://blog.10to1.be/cocoa/2012/01/27/creating-a-quick-look-plugin/
- http://apple.stackexchange.com/questions/153595/how-can-i-see-which-quicklook-plugin-is-responsible-for-which-data-type
- https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/Quicklook_Programming_Guide/Introduction/Introduction.html
