从 NSData 或 UIImage 查找图像类型
我正在从第三方提供的 URL 加载图像。 URL 上没有文件扩展名(或文件名)(因为它是一个模糊的 URL)。我可以从中获取数据(以 NSData 的形式)并将其加载到 UIImage 中并正常显示。
我想将这些数据保存到文件中。但是,我不知道数据是什么格式(PNG、JPG、BMP)?我假设它是 JPG(因为它是来自网络的图像),但是是否有一种编程方式可以确定?我查看了 StackOverflow 和文档,但没有找到任何东西。
TIA。
编辑:我真的需要文件扩展名吗?我将其保存到外部存储(Amazon S3),但考虑到它将始终在 iOS 或浏览器的上下文中使用(两者在没有扩展的情况下解释数据似乎都很好),也许这不是问题。
I am loading an image from a URL provided by a third-party. There is no file extension (or filename for that matter) on the URL (as it is an obscured URL). I can take the data from this (in the form of NSData) and load it into a UIImage and display it fine.
I want to persist this data to a file. However, I don't know what format the data is in (PNG, JPG, BMP)? I assume it is JPG (since it's an image from the web) but is there a programmatic way of finding out for sure? I've looked around StackOverflow and at the documentation and haven't been able to find anything.
TIA.
Edit: Do I really need the file extension? I'm persisting it to an external storage (Amazon S3) but considering that it will always be used in the context of iOS or a browser (both of whom seem fine in interpreting the data without an extension) perhaps this is a non-issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您有图像文件的 NSData,那么您可以通过查看第一个字节来猜测内容类型:
If you have NSData for the image file, then you can guess at the content type by looking at the first byte:
改进wl.的答案,这里有一种更扩展和更精确的方法来根据签名预测图像的MIME类型。该代码很大程度上受到 php 的 ext/standard/image 的启发。 c。
上述方法可识别以下图像类型:
image/x-ms-bmp
(bmp)image/gif
(gif)image/jpeg
( jpg、jpeg)image/psd
(psd)image/iff
(iff)image/webp
(webp)image/vnd. microsoft.icon
(ico)image/tiff
(tif, tiff)image/png
(png)image/jp2
(jp2 )不幸的是,没有简单的方法可以从
UIImage
实例获取此类信息,因为无法访问其封装的位图数据。Improving upon wl.'s answer, here's a much more extended and precise way to predict the image's MIME type based on the signature. The code was largely inspired by php's ext/standard/image.c.
The above method recognizes the following image types:
image/x-ms-bmp
(bmp)image/gif
(gif)image/jpeg
(jpg, jpeg)image/psd
(psd)image/iff
(iff)image/webp
(webp)image/vnd.microsoft.icon
(ico)image/tiff
(tif, tiff)image/png
(png)image/jp2
(jp2)Unfortunately, there is no simple way to get this kind of information from a
UIImage
instance, because its encapsulated bitmap data cannot be accessed.@Tai Le 针对 Swift 3 的解决方案是将整个数据分配到字节数组中。如果图像很大,可能会导致崩溃。该解决方案仅分配单个字节:
@Tai Le's solution for Swift 3 is assigning whole data into byte array. If an image large, it can cause crash. This solution just assigns single byte:
如果您从 URL 检索图像,那么您大概可以检查 HTTP 响应标头。
Content-Type
标头包含任何有用的内容吗? (我想这是因为浏览器可能能够正确显示图像,并且只有在适当设置内容类型的情况下才能做到这一点)If you're retrieving the image from a URL, then presumably you can inspect the HTTP response headers. Does the
Content-Type
header contain anything useful? (I'd imagine it would since a browser would probably be able to display the image correctly, and it could only do that if the content type were appropriately set)斯威夫特3版本:
Swift3 version:
接受答案的另一种选择是使用
image I/OframeWork
检查图像的UTI。您可以从 UTI 中获取图像类型。试试这个:
例如,GIF 图像的 UTI 是“com.compuserve.gif”,PNG 图像的 UTI 是“public.png”。但是您无法从
图像 I/O 框架
无法识别。An alternative of accepted answer is checking image's UTI with
image I/O frameWork
. You can achieve image type form UTI.try this:
For example, a GIF image's UTI is "com.compuserve.gif" and PNG image's UTI is "public.png".BUT you can't achieve UTI from image which
image I/O frameWork
doesn't recognized.要从
UIImage
获取图像类型,您可以从底层 Quartz 图像数据获取类型标识符 (UTI):要从 URL 获取图像类型标识符,它将取决于 URL 是否指向是否本地资源:
To get the image type from an
UIImage
you can get the type identifier (UTI) from the underlying Quartz image data:To get the image type identifier from a URL it will depend on whether the URL points to a local resource or not:
我基于@ccoroom改进的解决方案
一些使用示例:
My improved solution based on @ccoroom
Some usage examples:
如果这对你来说真的很重要,我相信你必须检查字节流。 JPEG 将以字节 FF D8 开始。 PNG 将以 89 50 4E 47 0D 0A 1A 0A 开头。我不知道 BMP 是否有类似的标头,但我认为您在 2010 年不太可能在网络上遇到这些标头。
但这对您来说真的很重要吗?难道您不能将其视为未知图像并让 Cocoa Touch 来完成工作吗?
If it really matters to you, I believe you'll have to examine the bytestream. A JPEG will start with the bytes FF D8. A PNG will start with 89 50 4E 47 0D 0A 1A 0A. I don't know if BMP has a similar header, but I don't think you're too likely to run into those on the web in 2010.
But does it really matter to you? Can't you just treat it as an unknown image and let Cocoa Touch do the work?
对每种已知的图像格式实施签名检查。下面是一个快速的 Objective-C 函数,可以对 PNG 数据执行此操作:
Implement a signature check for each known image format. Here is a quick Objective-C function that does that for PNG data:
我制作了一个库来检查 NSData 的图像类型:
https://github.com/sweetmandm/ImageFormatInspector
I made a library to check the image type of NSData:
https://github.com/sweetmandm/ImageFormatInspector