ios 从 NSData 确定 MIME 类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21789770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:26:34  来源:igfitidea点击:

Determine MIME type from NSData?

iosobjective-cmime-typesnsdata

提问by blee908

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.

您将如何确定 NSData 对象的 MIME 类型?我计划让用户从他们的 iPhone 上传视频/图片,并将该文件包装在 NSData 类中。

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 中分辨出 mime 类型。这个问题只有几个答案,最近的一个是 2010 年(4 年前!)。谢谢!

NSData *data; // can be an image or video
NSString *mimeType = [data getMimetype]; // how would I implement getMimeType

回答by Tib

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

根据ml 在类似帖子中的回答,我为 NSData 添加了 mime 类型确定:

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

PS:所有对应的 mime 类型都可以在这里找到

回答by Dmitry Isaev

?? 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"
    }
}

回答by Tom Harrington

Since you say that you're uploadingthis 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/jpegor 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.

既然您说要上传这些数据,您应该已经知道 MIME 类型。您创建了数据对象,您知道数据来自何处,并且 MIME 类型数量有限。因此,请使用适用于您的数据的任何一种。对于图像,它可能是image/jpegimage/png。对于视频,有很多video/类型。您可以在 Mac 上找到一长串 MIME 类型字符串/etc/apache2/mime.types。根据您拥有的视频类型,您需要一个或多个。

Determining the MIME type is a sticky problem; an NSDatacan 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 fileon your Mac and look in /usr/share/file/magic/to see how various file types are identified (the filecommand doesn't produce MIME types but it does analyze file contents to try and identify file types, so it's the same principle).

确定 MIME 类型是一个棘手的问题;anNSData可以编码任何类型的二进制数据。确定编码内容的唯一方法是检查字节。这反过来意味着对不同文件类型中存在哪些字节流有一定的了解。在许多(但不是全部)情况下应该可以使用查找字典,并且可能在某处有针对常见文件类型的开源实现。要了解所涉及的内容,请man file在 Mac 上尝试并/usr/share/file/magic/查看如何识别各种文件类型(该file命令不生成 MIME 类型,但它会分析文件内容以尝试识别文件类型,因此原理相同)。

回答by Manny

As far as I know, NSDatais a just a data object that wraps a byte array:

据我所知,NSData只是一个包装字节数组的数据对象:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html

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

...所以如果你想发现它的 MIME 类型,我能想到的一种方法是检查字节本身,然后从那里推测类型。这里有一个例子:从 NSData 或 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.

我还发现了这一点:确定从文件加载的 NSData 的 MIME 类型;它指的是一些可用于 MIME 类型查找的内部数据库(我猜)。

But like Tom Harrington says in his answer, it could get tricky depending on what you're dealing with.

但就像 Tom Harrington 在他的回答中所说的那样,根据您要处理的内容,它可能会变得棘手。

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.

当然,第二种解决方案依赖于您显然没有的文件扩展名,但我相信您已经注意到了。