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

时间:2023-01-15 18:51:38

Problem:

I would like to be able to use the built-in iOS icons for standard mime types (or UTI types) in my listing of binary file content.

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

Background:

I have looked into using the new (since 3.2) document architecture, but using the UIDocumentInteractionController it seems that the assumption is that the actual binaries are already on the local device.

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

In my case I have a file listing from a remote server and know the mime type, name, title, etc for the remote file so I just want to show a file listing with icons (the actual binary is only loaded as needed).

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

The meta data I get from the server contains proper mime types for the binaries so in theory I just want to get the system icon based on the type.

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

Work around?

I have tried the following "hack" as a proof of concept and it seems to work but this doesn't seem like the best way to go...

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

//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 个解决方案

#1


12  

You can create a UIDocumentInteractionController without needing to specify a URL. The header for the class says the icons are determined by name if set, URL otherwise.

您可以创建UIDocumentInteractionController而无需指定URL。该类的标题表示图标由名称确定(如果已设置),否则为URL。

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

#2


10  

I tried Ben Lings's solution, but it didn't work on iOS6.1 in either the simulator or on my iPad3. You need to provide an NSURL to the UIDocumentInteractionController, but that URL doesn't need to exist. Its last path component just needs to have the extension that you want.

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

The following code worked for me

以下代码对我有用

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;

#3


1  

So we are talking about hacks uh? I did this by doing some bad stuff, but it's working... I copied the icons from /system/library/frameworks/QuickLook.framework and added to my project. Inside this same folder, there is some property lists, that make the link between the UTI/extension/mime-type with the png file. With the plist and the pngs, all you have to do is make a logic to read the plists and show the correct png.

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

#1


12  

You can create a UIDocumentInteractionController without needing to specify a URL. The header for the class says the icons are determined by name if set, URL otherwise.

您可以创建UIDocumentInteractionController而无需指定URL。该类的标题表示图标由名称确定(如果已设置),否则为URL。

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

#2


10  

I tried Ben Lings's solution, but it didn't work on iOS6.1 in either the simulator or on my iPad3. You need to provide an NSURL to the UIDocumentInteractionController, but that URL doesn't need to exist. Its last path component just needs to have the extension that you want.

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

The following code worked for me

以下代码对我有用

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;

#3


1  

So we are talking about hacks uh? I did this by doing some bad stuff, but it's working... I copied the icons from /system/library/frameworks/QuickLook.framework and added to my project. Inside this same folder, there is some property lists, that make the link between the UTI/extension/mime-type with the png file. With the plist and the pngs, all you have to do is make a logic to read the plists and show the correct png.

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