在iOS中使用mime类型或UTI类型的内置图标

时间:2011-05-03 23:29:05

标签: iphone ios uidocumentinteraction

问题:

我希望能够在我的二进制文件内容列表中使用标准mime类型(或UTI类型)的内置iOS图标。

背景:

我已经研究过使用新的(自3.2)文档架构,但是使用UIDocumentInteractionController似乎假设实际的二进制文件已经在本地设备上。

在我的情况下,我有一个远程服务器的文件列表,并且知道远程文件的mime类型,名称,标题等,所以我只想显示带有图标的文件列表(实际二进制文件仅在需要时加载)。

我从服务器获取的元数据包含二进制文件的正确mime类型,所以理论上我只想根据类型获取系统图标。

解决方法?

我尝试了以下“黑客”作为概念证明,它似乎有效,但这似乎不是最好的方式......

//Need to initialize this way or the doc controller doesn't work
NSURL*fooUrl = [NSURL URLWithString:@"file://foot.dat"];
UIDocumentInteractionController* docController = [[UIDocumentInteractionController interactionControllerWithURL:fooUrl] retain];

UIImage* thumbnail = nil;
//Need to convert from mime type to a UTI to be able to get icons for the document
NSString *uti = [NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (CFStringRef)self.contentType, NULL)) autorelease];

//Tell the doc controller what UTI type we want
docController.UTI = uti;

//The doc controller now seems to have icon(s) for the type I ask for...
NSArray* icons = docController.icons;
if([icons count] > 0) {
    thumbnail = [icons objectAtIndex:0];
}
return thumbnail;

3 个答案:

答案 0 :(得分:12)

您无需指定网址即可创建UIDocumentInteractionController。该标题的标题表示图标由name确定(如果已设置),否则为URL

UIDocumentInteractionController* docController = [[UIDocumentInteractionController alloc] init];
docController.name = @"foo.dat";
NSArray* icons = docController.icons;
// Do something with icons
...
[docController release];

答案 1 :(得分:10)

我尝试了Ben Lings's solution,但它在模拟器或我的iPad3上都无法在iOS6.1上运行。您需要向NSURL提供UIDocumentInteractionController,但该网址不需要存在。它的最后一个路径组件只需要你想要的扩展名。

以下代码对我有用

NSString *extension = @"pptx"; // or something else
NSString *dummyPath = [@"~/foo" stringByAppendingPathExtension:extension]; // doesn't exist
NSURL *URL = [NSURL fileURLWithPath:dummyPath];
UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
NSArray *systemIconImages = documentInteractionController.icons;

return systemIconImages;

答案 2 :(得分:1)

所以我们谈的是黑客呃?我做了一些不好的事情,但它正在工作...... 我复制了/system/library/frameworks/QuickLook.framework中的图标并添加到我的项目中。在同一个文件夹中,有一些属性列表,它们在UTI / extension / mime-type和png文件之间建立链接。使用plist和pngs,你所要做的就是制作一个逻辑来读取plist并显示正确的png。