ios – 如何在Swift中的路径中找到文件UTI for file,withouth pa
发布时间:2020-12-14 19:27:31 所属栏目:百科 来源:网络整理
导读:我一直试图转换这个代码,我从这个 example(在Objective-c中)没有运气. String *path; // contains the file path // Get the UTI from the file's extension: CFStringRef pathExtension = (__bridge_retained CFStringRef)[path pathExtension]; CFStringRe
我一直试图转换这个代码,我从这个
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,NULL); let mimeType = UTTypeCopyPreferredTagWithClass(type,kUTTagClassMIMEType) if type != Null CFRelease(type) 我想要做的就是找出文件是图像还是视频文件 解决方法
您可以使用URL方法resourceValues(forKeys :)来获取文件URL TypeIdentifierKey,它返回文件统一类型标识符(UTI).您只需要检查返回的值是视频的“com.apple.m4v-video”,图像的“public.jpeg”或“public.png”等等.您可以向URL添加typeIdentifier属性扩展以返回TypeIdentifierKey字符串值,如下所示:
Xcode 9?Swift 4或Xcode 8.3.2?Swift 3.1 extension URL { var typeIdentifier: String? { return (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier } var localizedName: String? { return (try? resourceValues(forKeys: [.localizedNameKey]))?.localizedName } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |