是否有方法将全的不同 UTType扩展类型作为String获得?我需要他们专门用于图像,音频和视频。
我跟踪了这个答案,但它并没有给我所有的扩展
var types = [String]()
let utiTypes = [kUTTypeImage, kUTTypeMovie, kUTTypeVideo, kUTTypeMP3, kUTTypeAudio, kUTTypeQuickTimeMovie, kUTTypeMPEG, kUTTypeMPEG2Video, kUTTypeMPEG2TransportStream, kUTTypeMPEG4, kUTTypeMPEG4Audio, kUTTypeAppleProtectedMPEG4Audio, kUTTypeAppleProtectedMPEG4Video, kUTTypeAVIMovie, kUTTypeAudioInterchangeFileFormat, kUTTypeWaveformAudio, kUTTypeMIDIAudio, kUTTypeLivePhoto, kUTTypeTIFF, kUTTypeGIF, kUTTypeQuickTimeImage, kUTTypeAppleICNS]
for type in utiTypes {
let str = String(type)
guard let utiStr = fileExtension(for: str) else { continue }
types.appent(utiStr)
}
dump(types)结果是
15 elements // there are really 21 types
- "jpeg"
- "png"
- "mov"
- "mpg"
- "m2v"
- "ts"
- "mp3"
- "mp4"
- "mp4"
- "avi"
- "aiff"
- "wav"
- "midi"
- "tiff"
- "gif"这里的问题是它不返回像qt或jpg这样的值。例如,我使用UIDocumentPickerViewController,当我选择一个图像时,返回的url pathExtension是jpg而不是jpeg。如果我想知道返回的url是否是一个图像,并将其pathExtension与上面的类型数组进行比较,它会说它没有出现在列表中。
发布于 2021-11-24 20:44:08
你可以:
import UniformTypeIdentifiers
let utiTypes = [UTType.image, .movie, .video, .mp3, .audio, .quickTimeMovie, .mpeg, .mpeg2Video, .mpeg2TransportStream, .mpeg4Movie, .mpeg4Audio, .appleProtectedMPEG4Audio, .appleProtectedMPEG4Video, .avi, .aiff, .wav, .midi, .livePhoto, .tiff, .gif, UTType("com.apple.quicktime-image"), .icns]
print(utiTypes.flatMap { $0?.tags[.filenameExtension] ?? [] })当我在操场上运行这段代码时,UTTypes总共有33个文件扩展名。请注意,您列出的某些UTTypes没有与它们关联的文件扩展名,这可能是因为它们过于“通用”(例如“图像”和“视频”)。有些UTTypes有多个文件扩展名,有些可能与其他UTTypes的文件扩展名相同。
输出中没有"jpg“或"png”。要查看它们的出现,您必须使用以下列表:
// I've also removed the types that have no file name extensions
let utiTypes = [
UTType.jpeg,
.png,
.mp3,
.quickTimeMovie,
.mpeg,
.mpeg2Video,
.mpeg2TransportStream,
.mpeg4Movie,
.mpeg4Audio,
.appleProtectedMPEG4Audio,
.avi,
.aiff,
.wav,
.midi,
.tiff,
.gif,
UTType("com.apple.quicktime-image"),
.icns
]使用上面的列表,我的输出是:
jpeg
jpg
jpe
png
mp3
mpga
mov
qt
mpg
mpeg
mpe
m75
m15
m2v
ts
mp4
mpg4
mp4
mpg4
m4p
avi
vfw
aiff
aif
wav
wave
bwf
midi
mid
smf
kar
tiff
tif
gif
qtif
qti
icns还请注意,如果希望从文件扩展名中获取UTType,只需执行以下操作:
let type = UTType(tag: "jpg", tagClass: .filenameExtension, conformingTo: nil)并通过以下操作检查文件扩展名是否为例如图像的扩展名:
type?.isSubtype(of: .image)尽管请记住,该文件不一定仅仅因为其名称显示为:)而表示图像:)
发布于 2022-05-08 22:08:27
对于那些正在寻找所有可能类型的人来说--以下是iOS 15的完整列表:
func allUTITypes() -> [UTType] {
let types : [UTType] =
[.item,
.content,
.compositeContent,
.diskImage,
.data,
.directory,
.resolvable,
.symbolicLink,
.executable,
.mountPoint,
.aliasFile,
.urlBookmarkData,
.url,
.fileURL,
.text,
.plainText,
.utf8PlainText,
.utf16ExternalPlainText,
.utf16PlainText,
.delimitedText,
.commaSeparatedText,
.tabSeparatedText,
.utf8TabSeparatedText,
.rtf,
.html,
.xml,
.yaml,
.sourceCode,
.assemblyLanguageSource,
.cSource,
.objectiveCSource,
.swiftSource,
.cPlusPlusSource,
.objectiveCPlusPlusSource,
.cHeader,
.cPlusPlusHeader]
let types_1: [UTType] =
[.script,
.appleScript,
.osaScript,
.osaScriptBundle,
.javaScript,
.shellScript,
.perlScript,
.pythonScript,
.rubyScript,
.phpScript,
.makefile, //'makefile' is only available in iOS 15.0 or newer
.json,
.propertyList,
.xmlPropertyList,
.binaryPropertyList,
.pdf,
.rtfd,
.flatRTFD,
.webArchive,
.image,
.jpeg,
.tiff,
.gif,
.png,
.icns,
.bmp,
.ico,
.rawImage,
.svg,
.livePhoto,
.heif,
.heic,
.webP,
.threeDContent,
.usd,
.usdz,
.realityFile,
.sceneKitScene,
.arReferenceObject,
.audiovisualContent]
let types_2: [UTType] =
[.movie,
.video,
.audio,
.quickTimeMovie,
UTType("com.apple.quicktime-image"),
.mpeg,
.mpeg2Video,
.mpeg2TransportStream,
.mp3,
.mpeg4Movie,
.mpeg4Audio,
.appleProtectedMPEG4Audio,
.appleProtectedMPEG4Video,
.avi,
.aiff,
.wav,
.midi,
.playlist,
.m3uPlaylist,
.folder,
.volume,
.package,
.bundle,
.pluginBundle,
.spotlightImporter,
.quickLookGenerator,
.xpcService,
.framework,
.application,
.applicationBundle,
.applicationExtension,
.unixExecutable,
.exe,
.systemPreferencesPane,
.archive,
.gzip,
.bz2,
.zip,
.appleArchive,
.spreadsheet,
.presentation,
.database,
.message,
.contact,
.vCard,
.toDoItem,
.calendarEvent,
.emailMessage,
.internetLocation,
.internetShortcut,
.font,
.bookmark,
.pkcs12,
.x509Certificate,
.epub,
.log]
.compactMap({ $0 })
return types + types_1 + types_2
}注意事项:我有意将数据分成3个数组,以加快编译时间。
发布于 2021-11-25 16:30:24
您不应该尝试将UIDocumentPickerViewController返回的URL扩展与已知的扩展列表进行比较。相反,使用url.resourceValues(forKeys: [.contentTypeKey]).contentType为返回的UTType获取一个UTType,然后检查它是否符合.image:type.conforms(to: .image)。
https://stackoverflow.com/questions/70102279
复制相似问题