1
我用LZMA數據壓縮的基礎上NSData+Compression.swift的擴展,它的工作完全正常,直到iOS的10和自動轉換到斯威夫特3.自動轉換後的類看起來是這樣的:數據壓縮3
import Compression
extension Data {
static func compress(_ data: Data) -> Data {
let sourceBuffer = (data as NSData).bytes.bindMemory(to: UInt8.self, capacity: data.count)
let sourceBufferSize = data.count
let destinationBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: sourceBufferSize)
let destinationBufferSize = sourceBufferSize
let status = compression_encode_buffer(destinationBuffer, destinationBufferSize, sourceBuffer, sourceBufferSize, nil, COMPRESSION_LZMA)
if status == 0 {
print("Error with status: \(status)")
}
print("Original size: \(sourceBufferSize) | Compressed size: \(status)")
return Data(bytesNoCopy: UnsafeMutablePointer<UInt8>(destinationBuffer), count: status, deallocator: .free)
}
}
現在該方法的輸出總是爲空,壓縮大小爲0.你能幫我找出這個代碼有什麼問題嗎?
可能目標緩衝區太小。壓縮方法總是可以產生比輸入更多的輸出。 –
你可能想看看這個數據類型的[this](https://github.com/mw99/DataCompression/)Swift 3 LibCompression包裝器。它使用流功能,因此您不必擔心緩衝區大小。 – LimeRed