有没有一种实用的方法来压缩 NSData?
我没有看到任何有关该主题的文档,但这并不意味着它不存在。
I haven't seen any documentation on the topic, but that doesn't mean it doesn't exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
关注 @Zaph & @Brad Larson 的帖子,下面是 2 个方法
gzipInflate
和gzipDeflate
,它们可以很好地压缩/解压缩NSData
。 (代码重新格式化自 cocoadev.com/wiki/NSDataCategory从日志中:
Following @Zaph & @Brad Larson's posts, below are the 2 methods
gzipInflate
andgzipDeflate
that work just fine to compress/decompressNSData
. (code reformatted from cocoadev.com/wiki/NSDataCategoryFrom the log:
从 iOS 9.0 开始,内置了对更多压缩算法的支持。该库称为 libcompression,支持 LZ4、LZMA、ZLIB 和 LZFSE。
下面是一个使用 libcompression 解压 LZMA 的 Swift 示例。它很冗长,但避免了外部依赖,并且可以隐藏在
NSData
的扩展中。Starting with iOS 9.0, there is built-in support for a few more compression algorithms. The library is called libcompression and supports LZ4, LZMA, ZLIB and LZFSE.
Here’s a Swift example of using libcompression to decompress LZMA. It’s verbose, but avoids external dependencies and could be hidden in an extension on
NSData
.是的,用zlib压缩数据。
@Brad Larson 在此发布:参见此处并添加了代码。
有一个 CocoaPod 使用 Objective-Zip,作者:flyingdolphinstudio。
Yes, compress the data with zlib.
@Brad Larson posted on this: see here and added the code as well.
There is a CocoaPod which uses Objective-Zip by flyingdolphinstudio.
我制作了一个很好的 Objective-C BZip2 压缩接口,可作为 CocoaPod 使用: https://github.com/blakewatters/BZipCompression
I have made a nice Objective-C BZip2 compression interface available as a CocoaPod: https://github.com/blakewatters/BZipCompression
Swift 3 准备好 libcompression 的包装器。
https://github.com/mw99/DataCompression
在操场上玩起来很有趣:
游乐场压缩率
Swift 3 ready wrapper around libcompression.
https://github.com/mw99/DataCompression
Interesting to play with in the playground:
playground compression rates
在 iOS 13 和 macOS 10.15 或更高版本中,您可以使用
NSData
的新compressed
方法:不幸的是,此方法尚未移植到 Swift 的原生
Data
> 类,但可以通过在上述代码行后面添加as Data
来将NSData
简单地转换为Data
。In iOS 13 and macOS 10.15 or newer you can use the new
compressed
method ofNSData
:Unfortunately this method hasn't been ported to Swift's native
Data
class, butNSData
can be simply casted toData
by addingas Data
after the above code line.试试这个: https://github.com/mattt/Godzippa 这对我很有帮助。
Try this: https://github.com/mattt/Godzippa It was helpful for me.