- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Swift 压缩包解压缩 lz4 压缩的 png 图像,但代码以零大小退出。
我的代码如下,解压后的文件预计为240Kb。
[更新 2]
我在 Apple 的文档中偶然发现了这一点:
The frame is documented here so that you can easily wrap another LZ4 encoder/decoder to produce/consume the same data stream if necessary. An LZ4 encoded buffer is a sequence of blocks, each of which begins with a header. There are three possible headers:
A compressed block header consists of the octets 0x62, 0x76, 0x34, and 0x31, followed by the size in bytes of the decoded (plaintext) data represented by the block and the size (in bytes) of the encoded data stored in the block. Both size fields are stored as (possibly unaligned) 32-bit little-endian values. The compressed block header is followed immediately by the actual LZ4-encoded data stream.
An end of stream header consists of the octets 0x62, 0x76, 0x34, and 0x24 and marks the end of the LZ4 frame. No further data may be written or read beyond this header.
if let url = Bundle.main.url(forResource: "TestImage300.png", withExtension: "lz4") {
if let compressed = try? Data(contentsOf: url) {
if let decompressed = compressed.lz4Decompress() {
if let image = UIImage(data: decompressed) {
self.sourceImages.append(image)
print("Unittest: Decompressed image as \(image)")
}
}
}
}
import Foundation
import Compression
extension Data {
func lz4Decompress() -> Data? {
let destinationBufferSize: size_t = 480000
let destinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: destinationBufferSize)
let sourceBufferSize = count
let decompressed = withUnsafeBytes { (sourceBuffer: UnsafePointer<UInt8>) -> Data? in
let size = compression_decode_buffer(
destinationBuffer, destinationBufferSize,
sourceBuffer, sourceBufferSize,
nil, // scratch buffer automatically handled
COMPRESSION_LZ4
)
if size == 0 {
print("Error ")
return nil
}
print("Original compressed size: \(sourceBufferSize) | Decompressed size: \(size)")
return Data(bytesNoCopy: destinationBuffer, count: size, deallocator: .free)
}
return decompressed
}
}
最佳答案
let intArray: [Int8] = [-16, 1, 1, 39, 0, 19, 11, -30, 7, 10, 29, 14, 0, 0, 0, 0, 96, 9, 6, 0, 1, 2, 0, 17, 14, 6, 0, 2, 2, 0, 18, 14, 7, 0, 65, 0, 0, 0, -51, 6, 0, 0, 2, 0, 0, 43, 0, 16, 2, 9, 0, -1, 13, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 10, 4, 0, 64, 33, -105, 58, 115, 0, 12, 2, 0, 80, 0, 0, 0, 0, 0]
let uintArray = intArray.map { UInt8(bitPattern: $0) }
var encodedData = Data.init(bytes:uintArray)
let decodedCapacity = 205
let decodedDestinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: decodedCapacity)
let decodedData = encodedData.withUnsafeBytes {
(encodedSourceBuffer: UnsafePointer<UInt8>) -> Data? in
let decodedCharCount = compression_decode_buffer(decodedDestinationBuffer,
decodedCapacity,
encodedSourceBuffer,
encodedData.count,
nil,
COMPRESSION_LZ4_RAW)
if decodedCharCount == 0 {
fatalError("Decoding failed.")
}
print("Before: \(encodedSourceBuffer) | After: \(decodedCharCount)")
return Data(bytesNoCopy: decodedDestinationBuffer, count: decodedCharCount, deallocator: .free)
}
关于compression - 快速压缩 : doesn't decompress LZ4 image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47603583/
我正在使用 crc32(initial_crc_value,data,data_length); 生成一个 crc32 值。如果我没有在链接器选项中使用 -lz,我将收到链接器错误 "undefine
我正在尝试弄清楚如何证明用于压缩的 Lempel ZIV 77 算法确实提供了最佳压缩。 我找到了以下信息: So how well does the Lempel-Ziv algorithm wor
LZ-string 的哪种类型的 mysql 数据压缩结果?在我的数据库中,此变量为 MEDIUMTEXT 类型和排序规则 UTF8bin。使用post方法(asp.net core web serv
对于 xcode 中的其他链接器标志,-lz 意味着什么。我经常看到它与 -Objc 一起使用,或者它也可以独立使用吗?它的用途是什么?我正在使用 xcode 7.0.0 最佳答案 @Mozilla
我有兴趣实现用于压缩 ECG 信号的 LZ 算法,并希望优化与微 Controller 相关的代码。 这样压缩和解压缩 ECG 信号的熵效率更高,花费的时间更少。我完全不知道我是如何实现这一目标的。我
我正在尝试使用 quickfix 库编译代码: g++ -o main tradeclient.cpp Application.cpp -std=c++11 -fexceptions -finline
error: Ld /Users/mymac/Library/Developer/Xcode/DerivedData/Fitness-drawjjdksjmgttbwvhjuzicescoz/Buil
这让我发疯,当我尝试在模拟器上编译时,一切正常,但在设备上我遇到了这个错误: ld: library not found for -lz Command /Developer/Platforms/iP
我正在尝试在 Ubuntu 10.04 下编译 Android 源代码。我收到一条错误消息, /usr/bin/ld: cannot find -lz 你能告诉我如何解决它吗? cannot find
您好,我想在我的项目中使用 Google Maps API,我已经添加了 libz.dylib。当我在其他链接器标志中编写 -ObjC 以链接 Google Maps API 时,它工作正常但是当我删
我需要计算二进制字符串的 LZ 复杂度。 LZ 复杂度是从头到尾查看流时遇到的差异子串的数量。例如: s = 1001111011000010 在不同子串中标记序列复杂度c(s) = 6:s = 1/
在 ubuntu-13.04 上,使用 linux 发行版提供的 GCC-4.7.3 从共享库构建可执行文件时出现错误。 我猜问题出在libpng和zlib之间(前者使用后者),但我不知道为什么。 首
我知道这些是一些常见/基本的库,但它们到底是什么意思? 例如,我知道 -lm是一些数学库,但这是标准数学库还是什么? -lz压缩?什么压缩? 我不知道 -lrt 是什么是。 这些是什么东西? 数学库。
我想安装 uwsgi 但我无法安装。它是 Fedora 18 64 位系统。 python 是 2.7 版,yum install zlib 说:Package zlib-1.2.7-9.fc18.x
如果我解压缩 initrd.lz 然后重新打包而不做任何更改,然后将它替换为 Casper 目录中的新 initrd.lz for live CD。它是结构化的。在打包 initrd 时,我收到如下警
我按此顺序链接 CodeBlocks 中的库, -lz -L/usr/local/lib -L/usr/local/include -pthread -lswscale -lavutil -lavco
我不明白我的错误: LdLibrary/Developer/Xcode/DerivedData/Test-aywxyvnakaqhmwfbwellynwqmoik/Build/Products/Deb
我正在使用 lz-string.js 的 LZString.compressToBase64 函数并且需要在服务器端对数据进行解压/压缩。 明显的解决方案似乎是 lz_string_csharp但我很
尝试为 iOS 5 编译软件时,XCode 4.2 抛出错误: ld: library not found for -lz.1.2.3 我发现这篇文章告诉我要替换 1.2.3。与 1.2.5 http
我正在尝试从源代码“openssl-1.0.1j”进行编译,但失败并显示消息: “/usr/bin/ld: 找不到 -lz” 树莓派上的环境是 debian wheezy。 我的文件树: ├── zl
我是一名优秀的程序员,十分优秀!