Determine MIME type from NSData?

后端 未结 4 2033
深忆病人
深忆病人 2020-12-08 04:33

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

4条回答
  •  不思量自难忘°
    2020-12-08 05:34

    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.

提交回复
热议问题