Determine MIME type from NSData? Determine MIME type from NSData? ios ios

Determine MIME type from NSData?


Based on ml's answer from a similar post, I've added the mime types determination for NSData:

ObjC:

+ (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;}

Swift:

static func mimeType(for data: Data) -> String {    var b: UInt8 = 0    data.copyBytes(to: &b, count: 1)    switch b {    case 0xFF:        return "image/jpeg"    case 0x89:        return "image/png"    case 0x47:        return "image/gif"    case 0x4D, 0x49:        return "image/tiff"    case 0x25:        return "application/pdf"    case 0xD0:        return "application/vnd"    case 0x46:        return "text/plain"    default:        return "application/octet-stream"    }}

This handle main file types only, but you can complete it to fit your needs: all the files signature are available 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"    }}


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


matomo