How would you determine the mime type for an NSData object? I plan to have the user to upload a video/picture from their iPhone and have that file be wrapped in a NSData class.
I was wondering if I can tell the mime type from the NSData. There are only a few answers to this question and the most recent one is from 2010 (4 years ago!). Thanks!
NSData *data; // can be an image or video
NSString *mimeType = [data getMimetype]; // how would I implement getMimeType
Based on ml's answer from a similar post, I've added the mime types determination for NSData:
+ (NSString *)mimeTypeForData:(NSData *)data {
uint8_t c;
[data getBytes:&c length:1];
switch (c) {
case 0xFF:
return @"image/jpeg";
break;
case 0x89:
return @"image/png";
break;
case 0x47:
return @"image/gif";
break;
case 0x49:
case 0x4D:
return @"image/tiff";
break;
case 0x25:
return @"application/pdf";
break;
case 0xD0:
return @"application/vnd";
break;
case 0x46:
return @"text/plain";
break;
default:
return @"application/octet-stream";
}
return nil;
}
This handle main file types only, but you can complete it: you can find all the files signature here, just use the same pattern as I did.
PS: all the corresponding mime types are available here
❤️ Swift
extension Data {
private static let mimeTypeSignatures: [UInt8 : String] = [
0xFF : "image/jpeg",
0x89 : "image/png",
0x47 : "image/gif",
0x49 : "image/tiff",
0x4D : "image/tiff",
0x25 : "application/pdf",
0xD0 : "application/vnd",
0x46 : "text/plain",
]
var mimeType: String {
var c: UInt8 = 0
copyBytes(to: &c, count: 1)
return Data.mimeTypeSignatures[c] ?? "application/octet-stream"
}
}
As far as I know, NSData
is a just a data object that wraps a byte array:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html
...so one way I can think of if you wanted to discover its MIME type is to inspect the bytes themselves and then surmise the type from there. There's an example of that in here: Finding image type from NSData or UIImage
I also found this: Determine MIME Type of NSData Loaded From a File; which refers to some internal database (I guess) that can be used for a MIME type look-up.
But like Tom Harrington says in his answer, it could get tricky depending on what you're dealing with.
Edit...
Of course, that second solution relies on the file extension, which you obviously don't have, but I'm sure you noticed that already.
Since you say that you're uploading this data, you should already know the MIME type. You created the data object, you know where the data came from, and there are a limited number of MIME types. So use whichever one applies to your data. For an image it's probably image/jpeg
or image/png
. For videos there are a bunch of video/
types. You can find a long list of MIME type strings on your Mac in /etc/apache2/mime.types
. You'll want one or more of those depending on what kind of video you have.
Determining the MIME type is a sticky problem; an NSData
can encode any kind of binary data. The only way to determine what was encoded is to examine the bytes. That in turn means having some understanding of what byte streams exist in different file types. It should be possible to use a lookup dictionary in many (but not all) cases, and there might be an open source implementation somewhere for common file types. To get an idea of what's involved, try man file
on your Mac and look in /usr/share/file/magic/
to see how various file types are identified (the file
command doesn't produce MIME types but it does analyze file contents to try and identify file types, so it's the same principle).
var values = [UInt8](repeating:0, count:imgData.count)
imgData.copyBytes(to: &values, count:1)
switch values {
case 0xff:
return "image/jpeg"
case 0x89:
return "image/png"
case 0x47:
return "image/gif"
case 0x49, 0x4d:
return "image/tiff"
}
return ""