【发布时间】:2015-04-18 16:30:06
【问题描述】:
我一直在尝试转换我从example(在 Objective-c 中)获得的这段代码,但没有成功。
String *path; // contains the file path
// Get the UTI from the file's extension:
CFStringRef pathExtension = (__bridge_retained CFStringRef)[path pathExtension];
CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
CFRelease(pathExtension);
// The UTI can be converted to a mime type:
NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
if (type != NULL)
CFRelease(type);
我的代码是这样的
import MobileCoreServices
let path: String?
let pathExt = path.pathExtension as CFStringRef
let type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
let mimeType = UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType)
if type != Null
CFRelease(type)
我要做的就是找出文件是图像还是视频文件
【问题讨论】:
-
如果您查看您尝试移植的代码,以及您要移植的答案中的 cmets,您会注意到它并不能满足您的需要——它得到了文件名中的扩展名,然后查找该扩展名的 MIME 类型。如果您想要不带扩展名的类型信息,则需要查看文件的前几个字节以查找magic numbers。 iOS 不包含用于此的 API,但 there are third-party options.