获取Cocoa中的文件类型 [英] Get the type of a file in Cocoa
问题描述
我成功找到一个指定文件(JPEG图像,TIFF图像,...)的扩展文件类型,但我正在寻找一些更通用的,可以分类文件在大导管如图像,moovies,文本文件,...
有一种方法可以在可可(或目标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的列表, a href =http://developer.apple.com/library/mac/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html>此文档。
For a list of known UTIs, see this document.
这篇关于获取Cocoa中的文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!