Path extension and MIME file type in fast

I quickly learn, and I created a table with cells that show the image, and classes in groups by MIME type (using the path extension). I have a question about extensions. If the images (for example, it can be video or pdf) are taken from the Internet, I'm not sure if they have an extension or not, how can I get a MIMEtype file inside my file system without using the path extension? PS: Sorry for my poor English, this is not my native language.

+6
source share
3 answers

Assuming you get your data as NSData, follow this post: fooobar.com/questions/92444 / ...

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])") } 
+4
source

If someone wants to get MimeType from the actual file url, this code worked for me:

 import MobileCoreServices func mimeTypeForPath(path: String) -> String { let url = NSURL(fileURLWithPath: path) let pathExtension = url.pathExtension if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() { if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() { return mimetype as String } } return "application/octet-stream" } 
+23
source

To get the extension from UTI, enter the Swift 4.1 code:

 import AVFoundation extension AVFileType { /// Fetch and extension for a file from UTI string var fileExtension: String { if let ext = UTTypeCopyPreferredTagWithClass(self as CFString, kUTTagClassFilenameExtension)?.takeRetainedValue() { return ext as String } return "None" } } 

mov for com.apple.quicktime-movie

+1
source

All Articles