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



问题:

我希望能够在二进制文件内容列表中使用标准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;

您可以创建UIDocumentInteractionController,而无需指定URL。类的标题表示,如果设置了图标,则由name确定,否则由URL确定。

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

我尝试了Ben Lings的解决方案,但它在iOS6.1模拟器或iPad3上都不起作用。您需要向UIDocumentInteractionController提供一个NSURL,但该URL不需要存在。它的最后一个路径组件只需要具有您想要的扩展名。

以下代码适用于我

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;

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

最新更新