从 UIImage 获取底层 NSData
我可以使用 [UIImage imageWithData:]
或 [UIImage initWithData:]
方法从 NSData
创建 UIImage
。
我想知道是否可以从现有的 UIImage 中获取 NSData ? NSData *myData = [myImage getData]; 行上的内容
I can create UIImage
from NSData
using [UIImage imageWithData:]
or [UIImage initWithData:]
methods.
I wonder if I can get the NSData
back from an existing UIImage
?
Something on the line of NSData *myData = [myImage getData];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Objective-C
或
取决于您是否想要 PNG 格式或 JPG 格式的数据。
Swift
在 Swift 的现代版本中,上述方法已分别替换为
和
。
请注意,两者都返回一个可选值(
Data?
)。Objective-C
or
Depending if you want your data in PNG format or JPG format.
Swift
In modern versions of Swift, the methods above have been replaced with
and
respectively.
Note that both return an optional value (
Data?
).当使用
init(data: originalData)
初始化UIImage
对象时,originalData
将被转换为某种内部格式的原始数据。稍后可以使用rawData
检索这些数据,但是由于
rawData
是原始数据,因此它会比使用UIImagePNGRepresentation
时更大。When initialising a
UIImage
object withinit(data: originalData)
, thatoriginalData
will be converted into raw data in some kind of internal format. These data can be retrieved later withHowever because the
rawData
is raw, it is going to be even larger than when usingUIImagePNGRepresentation
.斯威夫特4.2
Swift 4.2
只是因为我偶然发现了这一点,而且我喜欢 swift :)
这是 Caroline 帖子的快速翻译。
或者
Just because I stumbled upon this and i like swift :)
Here is the swift translation of Caroiline's post.
Or
您可以预期 UIImage 是一个为显示而格式化的对象,因此不会使用原始数据(可能是 PNG 或 JPEG 格式),而更可能是像素数组或其他内部格式。换句话说,
UIImage(data: foo)
不会保留foo
。如果你只是想在程序的其他地方使用它,原始的 UIImage 就可以了(我想这里实际上并非如此)
如果你想序列化,
UIImagePNGRepresentation(...)
可以工作,但如果原始图像是 JPEG,则尺寸会过大;UIImageJPEGRepresentation(...)
通常会导致数据稍微过大,并且如果您的原始数据是 PNG,则数据会略有丢失。应该可以根据图像的显示方式和您期望提供的格式来选择一种。如果您碰巧使用 PNG 输入并希望输出 PNG,您应该获得良好的文件大小和几乎相同的数据,除了特殊的 PNG 块。如果您想获得原始数据的精确副本(也许在缩略图后保存文件,或对其进行 SHA1 处理),则需要单独保留它。你可能会这样做:
You can expect that a UIImage is an object formatted for display and so won't be using the original data (which is probably in PNG or JPEG format) but more likely a pixel array or some other internal format. In other words,
UIImage(data: foo)
will not retainfoo
.If you just want to use it elsewhere in your program, the original UIImage will do fine (I presume that's not actually the case here)
If you want to serialise,
UIImagePNGRepresentation(...)
will work but will be oversized if the original was a JPEG;UIImageJPEGRepresentation(...)
will often result in slightly oversize data and is slightly lossy if your original was PNG. It should be okay to pick one based on the way the image will be displayed and the format you expect to be provided. If you happen to be using PNG in and want PNG out, you should get a good file size and almost identical data, special PNG chunks aside.If you want to get an exact copy of the original data (perhaps to save a file after thumbnailing, or to SHA1 it), then you need to retain it separately. You might do something like:
我发现序列化/反序列化 UIImage(通过数据)的唯一解决方案是使用此解决方案。
然后,无论 UIImage 是如何创建的,您都可以通过使用 UIImage 上的扩展方法来序列化/反序列化:
The only solution I found to serialize/unserialize a UIImage (via Data) is by using this solution.
You can then serialize/unserialize regardless of how the UIImage was created by using the extension method on UIImage:
自从给出上述答案以来,事情已经发生了变化,对于那些仍在寻找的人来说,因为他们分享 CipherCom 的担忧:iOS 5 添加了 CIImage 属性。
Things have changed since the above answer was given, for those still looking because they share CipherCom's concern: iOS 5 has added the CIImage property.