How to Find File Uti for File, Withouth Pathextension, in a Path in Swift

How do I get a document UTI from a file path?

You will be able to get it using mobile core services framework. Refer the code below

NSString *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 code snippet is taken from here.

Path extension and MIME type of file in swift

Assuming you are receiving your data as NSData, follow this Post: https://stackoverflow.com/a/5042365/2798777

In Swift for example:

var c = [UInt32](count: 1, repeatedValue: 0)
(data as! NSData).getBytes(&c, length: 1)
switch (c[0]) {
case 0xFF, 0x89, 0x00:
println("image")
case 0x47:
println("gif")
default:
println("unknown: \(c[0])")
}

Get the type of a file in Cocoa

You could use Uniform Type Identifiers. Since UTIs are organised into hierarchies, one possibility is to check whether the preferred UTI for a given file conforms to a top-level UTI, e.g. public.image for images or public.movie for movies. The Core Services framework includes functions that operate on UTIs as well as constants representing known UTIs.

For instance, working on file name extensions:

NSString *file = @"…"; // path to some file
CFStringRef fileExtension = (CFStringRef) [file pathExtension];
CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);

if (UTTypeConformsTo(fileUTI, kUTTypeImage)) NSLog(@"It's an image");
else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) NSLog(@"It's a movie");
else if (UTTypeConformsTo(fileUTI, kUTTypeText)) NSLog(@"It's text");

CFRelease(fileUTI);

If you have a MIME type instead of a file name extension, you can use kUTTagClassMIMEType instead of kUTTagClassFilenameExtension.

For a list of known UTIs, see this document.

How to get All Extensions for UTType Image, Audio, and Video

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 :)

How can I get the file type description of any file using Objective-C?

You can use -[NSURL resourceValuesForKeys:error:] to ask for the localized type description of the file:

#import 

int main(int argc, char *argv[]) {
@autoreleasepool {
NSURL *url = [NSURL fileURLWithPath:@"/bin/echo"];

NSError *error = nil;
NSDictionary *values = [url resourceValuesForKeys:@[NSURLLocalizedTypeDescriptionKey] error:&error];

NSString *description = values[NSURLLocalizedTypeDescriptionKey];
if (!description) {
NSLog(@"Failed to get description: %@", error);
} else {
NSLog(@"%@", description);
}
}
}

On my system, this produces the same "Unix executable" value that you see in the Finder.


In Swift:

import Foundation

let url = URL(fileURLWithPath: "/bin/echo")
let values = try url.resourceValues(forKeys: [.localizedTypeDescriptionKey])
print(values.localizedTypeDescription) // Optional("Unix executable")


Related Topics



Leave a reply



Submit