简体   繁体   English

从NSData或编码的图像中提取图像类型

[英]Extract image type from NSData or encoded image

Background 背景

I have a function where a user can upload and send over an image from my ios to my rails app and it works for .jpg files. 我有一个功能,用户可以将图像从ios上传并发送到我的rails应用程序,它适用于.jpg文件。 I would like to make it work for all image types. 我想使其适用于所有图像类型。 All I need to do is send over the image type in the API POST. 我需要做的就是通过API POST中的图片类型发送。

So far the user uploads the image and its an NSData, then I encoded into a image.base64EncodedStringWithOptions and put it into a JSON and send it over. 到目前为止,用户已上传图像及其NSData,然后我将其编码为image.base64EncodedStringWithOptions并将其放入JSON并发送过来。 This works for .jpg's. 这适用于.jpg。

Question

How do I get the image's type from the NSData or the encoded string of an image? 如何从NSData或图像的编码字符串中获取图像的类型?

Examples 例子

Here is the NSData of a very small .png that the user might try to upload. 这是用户可能尝试上传的非常小的.png的NSData

<89504e47 0d0a1a0a 0000000d 49484452 0000000a 0000000a 08060000 008d32cf bd000000 01735247 4200aece 1ce90000 00097048 59730000 16250000 16250149 5224f000 00001c69 444f5400 00000200 00000000 00000500 00002800 00000500 00000500 00005ec1 07ed5500 00002a49 44415428 1562f88f 04181818 fea36398 34038c01 a2d11581 f8308060 11ab109b 691826e2 5284ac10 000000ff ff232a1e 6b000000 27494441 5463f80f 040c0c0c 3831481e 0418c004 0e856015 5002a742 644560c3 c0041613 c9560800 782fe719 4293f838 00000000 49454e44 ae426082>

Here is output of 这是输出

strBase64:String = image.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
print(strBase64)

iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAAXNSR0IArs4c6QAA\\r\\nAAlwSFlzAAAWJQAAFiUBSVIk8AAAABxpRE9UAAAAAgAAAAAAAAAFAAAAKAAAAAUA\\r\\nAAAFAAAAXsEH7VUAAAAqSURBVCgVYviPBBgYGP6jY5g0A4wBotEVgfgwgGARqxCb\\r\\naRgm4lKErBAAAAD//yMqHmsAAAAnSURBVGP4DwQMDAw4MUgeBBjABA6FYBVQAqdC\\r\\nZEVgw8AEFhPJVggAeC/nGUKT+DgAAAAASUVORK5CYII=

You should check your Base 64 encoded string for magic numbers in files . 您应该检查Base 64编码的字符串magic numbers in files
https://en.m.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files https://en.m.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files

For example for PNG : 例如对于PNG

PNG image files begin with an 8-byte signature which identifies the file as a PNG file and allows detection of common file transfer problems: \\211 PNG \\r \\n \\032 \\n (89 50 4E 47 0D 0A 1A 0A). PNG图像文件以8字节签名开头,该签名将文件标识为PNG文件,并允许检测常见的文件传输问题:\\ 211 PNG \\ r \\ n \\ 032 \\ n(89 50 4E 47 0D 0A 1A 0A)。

For JPEG : 对于JPEG

JPEG image files begin with FF D8 and end with FF D9. JPEG图像文件以FF D8开头,以FF D9结尾。 JPEG/JFIF files contain the ASCII code for "JFIF" (4A 46 49 46) as a null terminated string. JPEG / JFIF文件包含“ JFIF”(4A 46 49 46)的ASCII代码,为空终止字符串。 JPEG/Exif files contain the ASCII code for "Exif" (45 78 69 66) also as a null terminated string, followed by more metadata about the file. JPEG / Exif文件包含“ Exif”(45 78 69 66)的ASCII码,也为空终止字符串,其后是有关该文件的更多元数据。

So parse your string, detect matching of any magic number , map magic number to image file type. 因此,解析您的字符串,检测任何magic number匹配,将magic number映射到图像文件类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM