【问题标题】:How to save image in memory and how to zip all the images in swift?如何将图像保存在内存中以及如何快速压缩所有图像?
【发布时间】:2019-05-22 10:16:42
【问题描述】:

在我们的项目中,当我们捕获图像时,我们将图像存储在本地应用程序路径中,并且它采用加密格式。并添加一些信息。最后,我们将所有图像压缩并发送到服务器,这是我们当前的流程。

我们要做的是将图像转换为数据并编码为 base64 格式。但是如何压缩所有图像(我们需要捕获超过 5 张图像)。

压缩所有base64格式字符串并发送到服务器。怎么做?

您是否知道将图像保存在内存中并将所有这些图像压缩并发送到服务器而不是将图像存储在应用文件夹中?

【问题讨论】:

  • 为什么要压缩 base64 字符串?为什么不压缩实际的图像数据?
  • 当我们解压文件目录中的图片时。我们可以看到这些图像。我们有带有渗透测试的测试应用程序。渗透测试人员轻松更改我的原始图像并将假图像发送到服务器。因此,出于安全目的,我们计划不保存在目录中。如何将图像保存在内存中?

标签: ios swift memory base64 zip


【解决方案1】:

1- 将每个图像转换为数据并编码为base64格式并压缩:

let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
print(strBase64)

使用 chilkat 压缩字符串:https://www.example-code.com/swift/compress_decompress_string.asp

func chilkatTest() {
//  This example requires the Chilkat API to have been previously unlocked.
//  See Compression UnlockComponent for sample code.

var success: Bool
let sb = CkoStringBuilder()

var i: Int
for i = 1; i <= 20; i++ {
    sb.Append("This is the original uncompressed string.\r\n")
}

let compress = CkoCompression()
compress.Algorithm = "deflate"
//  Indicate that the utf-8 byte representation of the string should be compressed.
compress.Charset = "utf-8"

var compressedBytes: NSData
compressedBytes = compress.CompressString(sb.GetAsString())

//  If the compressed data is desired in string format, then get the base64 representation of the bytes.
compress.EncodingMode = "base64"
var compressedBase64: String? = compress.CompressStringENC(sb.GetAsString())
print("Compressed Bytes as Base64: \(compressedBase64!)")

//  Now decompress...
var decompressedString: String? = compress.DecompressString(compressedBytes)
print("The original string after decompressing from binary compressed data:")
print("\(decompressedString!)")

//  To decompress from Base64...
compress.EncodingMode = "base64"
decompressedString = compress.DecompressStringENC(compressedBase64)
print("The original string after decompressing from Base64:")
print("\(decompressedString!)")

}

2- 以 json 格式保存:

   [
     {"1": "strBase64FromStep1"},
     {"2": "strBase64FromStep1"},
      .
      .
      .
   ]

3- 将 json 字符串保存到文本文件:

let file = "file.txt" //this is the file. we will write to and read from it

let text = "some text" //just a text

if let dir = FileManager.default.urls(for: .documentDirectory, in: 
    .userDomainMask).first {

    let fileURL = dir.appendingPathComponent(file)

    //writing
    do {
        try text.write(to: fileURL, atomically: false, encoding: .utf8)
    }
    catch {/* error handling here */}

    //reading
    do {
        let text2 = try String(contentsOf: fileURL, encoding: .utf8)
    }
    catch {/* error handling here */}
}

4- 将您的文件 url 保存到 UserDefaults :

fileURL From step 3 

NSUserDefaults.standardUserDefaults().setObject(fileURL, forKey: "path")
NSUserDefaults.standardUserDefaults().synchronize()

5 - 随时使用此文件发送到服务器。

【讨论】:

  • 感谢您的评论@Ammar。我会检查并通知您。
  • 嗨,ammar,我按照 chilkat 程序进行操作,但在桥接错误中找不到“CkoJsonObject.h”文件。
  • 嗨,阿马尔。我得到 "Compressed Bytes as Base64: nil" nil value.
  • 请检查我的回答 Ammar
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多