ios - 如何在swift 3中压缩数据?

标签 ios swift3 base64 compression nsdata

我在 Swift 3 的文件管理器中有一些文件。我想上传它们,但是当我将它们转换为 base 64 时,它们的大小会很大!所以我想在将数据转换为 base 64 之前压缩数据。

这是我的转换代码:

for i in 0...(rows?.count)! - 1 {

   let filePath = filesurl[fileManagerViewController.selectedFileIndex[i]]
        do {
            let fileData = try Data.init(contentsOf: filePath)

            let fileStream:String = fileData.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))

            fileManagerViewController.upupload.append(fileStream)


        } catch {
            print(error.localizedDescription)
        }


        }

我用过

let compressedData = fileData(UF_COMPRESSED)

但这对我不起作用,所以请帮我压缩文件,然后再将它们转换为 base 64 以供上传。

最佳答案

这是用 Swift 3 编写的 libcompression 包装器。 https://github.com/mw99/SwiftDataCompression

Swift libcompression wrapper as an extension for the Data type (ZLIB, LZFSE, LZMA, LZ4, deflate, RFC-1950, RFC-1951)

所以你可以这样压缩你的数据:

let fileData = try Data.init(contentsOf: filePath)
let compressedData = fileData.compress(withAlgorithm: .LZFSE)

关于ios - 如何在swift 3中压缩数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45657962/

相关文章:

ios - Array.index(: can return nil But there's compile error when checking for optional value

c# - Base-64 字符数组的长度无效

objective-c - 为什么我的数据可以成功写入 iPhone 模拟器上的文件,而不是我的设备?

iphone - 使用新版本更新时,束路径是否会更改?

ios - 如何在swift 3中绘制圆弧

ios - Xcode 8 和 Swift 3 中 Unresolved 标识符错误

javascript - 如何在TinyMCE中将图像编码为base64?

angularjs - 无法在 asp.net mvc 中显示来自 s3 存储桶的上传图像

ios - Mac和iOS Safari src播放html5视频的路径要求?

iphone - UILabel 未使用另一个类的值进行更新