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 cla
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).