设为首页 收藏本站
查看: 1062|回复: 0

[经验分享] Mac OS X平台下QuickLook开发教程

[复制链接]

尚未签到

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


二、着手开发
  QuickLook插件是Apple官方推出的一项技术,在XCode中可以直接创建QuickLook Plugins模板工程:
DSC0002.png DSC0003.png

  创建的模板工程中包含三个源文件: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文件中去即可。
DSC0004.png DSC0005.png DSC0006.png

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

  (3) 需要注意的是,在GeneratePreviewForURL()GenerateThumbnailForURL()方法中传进来的文件路径以URL形式存在的。也就是说,当路径中存在中文时,会进行URL Encode编码。也就是说,中文"直播间"会变成"%E7%9B%B4%E6%92%AD%E9%97%B4"。在解析的时候要注意进行URL Decode操作,否则的话无法读取到文件。
  (4)qlmanage的使用。qlmanage可以用于清除QuickLook缓存,也可以用来查看当前系统中存在哪些QuickLook插件,也可以查看哪些文件是和哪些QuickLook插件关联的。这对于判断你的QuickLook插件是否起作用很方便:
DSC0008.png

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




    • 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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-390902-1-1.html 上篇帖子: Mac系统安装Aircrack-ng破解附近wifi密码(1) 下篇帖子: Mac中Eclipse安装和使用svn
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表