Is there a way to get All of the different UTType extension types as Strings? I need them specifically for images, audio, and video.
I followed this answer, but it doesn't give me all of the extensions
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)
The results are
15 elements // there are really 21 types
- "jpeg"
- "png"
- "mov"
- "mpg"
- "m2v"
- "ts"
- "mp3"
- "mp4"
- "mp4"
- "avi"
- "aiff"
- "wav"
- "midi"
- "tiff"
- "gif"
The issue here is it doesn't return values like qt or jpg. For example I use the UIDocumentPickerViewController and when I select an image the returned url pathExtension is jpg not jpeg. If I wanted to know if the returned url was an image, and I compared its pathExtension to the types array above, it would say that it doesn't appear in the list.
You can do:
There are 33 file extensions in total for the UTTypes that you have listed when I run this code in a playground. Note that some UTTypes you have listed have no file name extensions associated with them, probably because they are too "generic" (e.g. "image" and "video"). And some UTTypes have multiple file name extensions, and some may be the same with the file name extensions of other UTTypes.
There is no "jpg" or "png" in the output. To see them appear, you will have to use this list:
Using the above list, the output for me is:
Also note that if you want to get the UTType from a file name extension, you can just do:
and check whether the file name extension is e.g. that of an image by doing:
Though bear in mind that the file does not necessarily represent an image just because its name says it is :)