iPhone 上的压缩 API
是否有可在 iPhone 上使用的压缩 API? 我们正在为我们的 iPhone 应用程序构建一些 RESTful Web 服务来进行对话,但我们希望至少压缩一些对话以提高效率。
我不在乎格式(ZIP、LHA,等等)是什么,也不需要是安全的。
一些受访者指出,服务器可以压缩其输出,iPhone 可以使用它。 我们的情况恰恰相反。 我们将把压缩内容发布到网络服务。 我们不关心相反的压缩。
Is there a compression API available for use on the iPhone? We're building some RESTful web services for our iPhone app to talk to, but we want to compress at least some of the conversations for efficiency.
I don't care what the format (ZIP, LHA, whatever) is, and it does not need to be secure.
Some respondents have pointed out that the server can compress its output, and the iPhone can consume that. The scenario we have is exactly the reverse. We'll be POSTing compressed content to the web service. We're not concerned with compression going the other way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您将对话数据存储在 NSData 对象中,CocoaDev wiki 上的人员已经发布了 NSData类别,添加 gzip 和 zlib 压缩/解压缩作为简单方法。 这些在我的 iPhone 应用程序。
由于上面的链接已经失效,而 CocoaDev wiki 正在转移到新的主机,所以我在下面完整地复制了这个类别。
接口:
实现:
If you store the data for the conversations in an NSData object, the folks at the CocoaDev wiki have posted an NSData category that adds gzip and zlib compression / decompression as simple methods. These have worked well for me in my iPhone application.
As the above link has gone dead while the CocoaDev wiki is being moved to a new host, I've reproduced this category in its entirety below.
Interface:
Implementation:
zlib 和 bzip2 可用。 您可以随时添加其他文件,只要它们(通常)在 OS X 下编译即可。
对于最小文件大小,bzip2 是更好的选择,但需要更多的 CPU 能力来压缩和解压缩。
另外,由于您正在与网络服务通信,因此您可能不需要做太多事情。 NSURLRequest 在服务器响应中接受 gzip 编码透明。
zlib and bzip2 are available. And you can always add others, as long as they'll (generally) compile under OS X.
bzip2 is a better choice for smallest file sizes, but requires much more CPU power to compress and decompress.
Also, since you're talking to a web service, you may not have to do much. NSURLRequest accepts gzip encoding transparently in server responses.
Apple 的内置 libcompression 现在可用于 iOS 9。下面显示了用于压缩 NSData 的 compression_encode_buffer 的简短示例。
有多种不同的算法可供使用:
块压缩或流压缩均受支持。
请参阅 https://developer.apple.com/library/mac/documentation /性能/参考/压缩/
Apple's built in libcompression is now available for iOS 9. A short example of compression_encode_buffer is shown below for compressing NSData.
A number of different algorithms are available:
Block compression or stream compression are both supported.
See https://developer.apple.com/library/mac/documentation/Performance/Reference/Compression/
如果您只有压缩数据并且知道未压缩的大小,您可以使用:
If you have only compressed data and know uncompressed size you can use:
我相信 zlib 在手机上可用。
I believe zlib is available on the phone.
相信我,最好的选择是使用 ZipArchive,请参阅:如何解压缩 AES -256 加密的 Zip 文件?
准备好在需要时寻求帮助。
trust me the best choice is to use ZipArchive see: How to decompress an AES-256 encrypted Zip file?
ready for help if needed.
Objective-Zip 是另一种选择。 请参阅这些优秀的说明。
注意我必须使用 XCode->Edit->Refactor->Convert to Objective C ARC 将源代码转换为使用 ARC。
Objective-Zip is another option. See these excellent instructions.
Note I had to convert the source code to use ARC by using the XCode->Edit->Refactor->Convert to Objective C ARC.
NSURL 表示它支持 gzip 编码,因此您只需让 RESTful Web 服务在适当的时候返回 gzip 编码内容即可。 所有解码都将在幕后完成。
NSURL says it supports the gzip encoding so you shouldn't have to do anything more than have your RESTful web service return gzip encoded content when appropriate. All the decoding will be done under the covers.