问题描述
我成功地找到了指定文件的扩展文件类型(JPEG 图像、TIFF 图像等),但我正在寻找更通用的东西,可以将文件分类为大类别",如图像、moovies、文本文件, ...有没有办法在可可(或 Objective-c)中实现这一点?
I succeeded to find the extended file type of a specified file ( JPEG image, TIFF Image, ...) but I am looking for something more generic that can categorize files in "big cathegories" like images, moovies, text files, ...It there a way to acheive this in cocoa (or objective-c) ?
感谢您的帮助,
推荐答案
你可以使用 统一类型标识符.由于 UTI 被组织成层次结构,一种可能性是检查给定文件的首选 UTI 是否符合顶级 UTI,例如public.image
用于图像或 public.movie
用于电影.核心服务框架包括对 UTI 进行操作的函数以及表示已知 UTI 的常量.
You could use Uniform Type Identifiers. Since UTIs are organised into hierarchies, one possibility is to check whether the preferred UTI for a given file conforms to a top-level UTI, e.g. public.image
for images or public.movie
for movies. The Core Services framework includes functions that operate on UTIs as well as constants representing known UTIs.
例如,处理文件扩展名:
For instance, working on file name extensions:
NSString *file = @"…"; // path to some file
CFStringRef fileExtension = (CFStringRef) [file pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(@"It's an image");
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(@"It's a movie");
else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(@"It's text");
CFRelease(fileUTI);
如果您有 MIME 类型而不是文件扩展名,则可以使用 kUTTagClassMIMEType
而不是 kUTTagClassFilenameExtension
.
If you have a MIME type instead of a file name extension, you can use kUTTagClassMIMEType
instead of kUTTagClassFilenameExtension
.
有关已知 UTI 的列表,请参阅 本文档.
For a list of known UTIs, see this document.
这篇关于在 Cocoa 中获取文件的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!