0

In our project when we captured an image we stored the image in local app path and it's in an encrypted format. And add some information. Finally, we have zip all the image and send it server, this is our current flow.

What we are trying to do is we convert an image into data and encoded a base64 format. But how to zip all the images(we need to capture more than 5 images).

Zip all the base64 format string and send it to the server . How to do that?

Do u have any idea about to save the image in memory and zip all those images and send to the server instead of the store an image in app folder?

HariKarthick
  • 1,369
  • 1
  • 19
  • 47
  • Why zip the base64 strings? Why not zip the actual image data? – rmaddy Dec 22 '18 at 05:37
  • When we unzip the image in document directory. we can able to see those images. We have ttesting app with pen test. The pen testers easily change my original image and send fake image to the server. So that we are planning to not save in directory for security purpose. How to save image in memory? – HariKarthick Dec 22 '18 at 05:44

1 Answers1

0

1- convert each image into data and encoded a base64 format and zip it :

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

zip string using 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- save it in json format :

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

3- save the json String to text file :

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- save your file url to UserDefaults :

fileURL From step 3 

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

5 - use this file any time you want to send to server .

Ammar
  • 380
  • 6
  • 16
  • Thanks for your comment @Ammar. I will check and let you know. – HariKarthick Dec 22 '18 at 07:54
  • HI ammar, I followed the chilkat procedure but 'CkoJsonObject.h' file not found in brdging error. – HariKarthick Dec 22 '18 at 09:43
  • 1
    look at this https://stackoverflow.com/questions/31716413/xcode-not-automatically-creating-bridging-header and this https://www.codementor.io/codementorteam/swift-objective-c-using-2-languages-in-your-project-ucm16ki0l – Ammar Dec 22 '18 at 09:49
  • hi Ammar. I am getting **"Compressed Bytes as Base64: nil "** nil value. – HariKarthick Dec 24 '18 at 10:35
  • Please check my answer Ammar – HariKarthick Dec 24 '18 at 11:56
  • I just checked with the server team. They said it's not correct format. I stored JSON string link below code. – HariKarthick Dec 24 '18 at 11:58
  • Optional("ChilkatLog:\n WriteEntireFile:\n DllDate: Dec 21 2018\n ChilkatVersion: 9.5.0.76\n UnlockPrefix: Anything for 30-day trial\n Architecture: Little Endian; 64-bit\n Language: IOS C/C++/Swift/Objective-C\n VerboseLogging: 0\n fopen_failed:\n errno: 1\n osErrorMessage: Operation not permitted\n Failed to open file.\n mode: w\n path: AllFiless.zip\n --fopen_failed\n filename: AllFiless.zip\n Failed to write complete file (7)\n --WriteEntireFile\n--ChilkatLog\n") – HariKarthick Dec 26 '18 at 07:14