1

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.

4

2 回答 2

1

You can do:

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] ?? [] })

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:

// 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
]

Using the above list, the output for me is:

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

Also note that if you want to get the UTType from a file name extension, you can just do:

let type = UTType(tag: "jpg", tagClass: .filenameExtension, conformingTo: nil)

and check whether the file name extension is e.g. that of an image by doing:

type?.isSubtype(of: .image)

Though bear in mind that the file does not necessarily represent an image just because its name says it is :)

于 2021-11-24T20:44:08.333 回答
0

You should not try to compare the extension of the URL returned by UIDocumentPickerViewController to a known list of extensions. Instead, use url.resourceValues(forKeys: [.contentTypeKey]).contentType to get a UTType for the returned URL, and then check that it conforms to .image: type.conforms(to: .image).

于 2021-11-25T16:30:24.453 回答