8

I have an Android app which uses Base64 to encode images, and encoded strings are stored on a server. I am now making an iOS client for the same app and am struggling to make it encode images in the same way Images encoded on the android end will decode in Swift iOS but images encoded in Swift will NOT decode in Android, or here http://www.freeformatter.com/base64-encoder.html (the resulting file isn't a valid image)

Images encoded in iOS WILL decode in iOS

In Android, I am using the following to encode and decode

public static String encodeBitmap(Bitmap bitmap) {
    Bitmap immagex = bitmap;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immagex.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

public static Bitmap decodeBitmap(String encodedString) {
    byte[] decodedByte = Base64.decode(encodedString, Base64.DEFAULT);
    Bitmap b = BitmapFactory.decodeByteArray(decodedByte, 0,
            decodedByte.length);
    return b;
}

And the following on the iOS side

static func decodeImage(str: String) -> UIImage?{
    if let decodedData = NSData(base64EncodedString: str, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters){
        var iconValue:UIImage? = UIImage(data: decodedData)
        return iconValue
    }
    return nil
}

static func encodeImage(image: UIImage) -> String{
    var imageData = UIImagePNGRepresentation(image)
    let base64 = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding76CharacterLineLength)
    return base64
}

}

I am willing to change either client to make it work

Example: take this image for example https://pbs.twimg.com/profile_images/522909800191901697/FHCGSQg0.png

On Android it encodes to http://pastebin.com/D41Ldjis

And on iOS to http://pastebin.com/fEUZSJvF

iOS one has a much larger character count

Hamzah Malik
  • 2,540
  • 3
  • 28
  • 46
  • every example I see with swift shows this `imageData.base64EncodedStringWithOptions(.allZeros)` – tyczj Aug 18 '15 at 21:09
  • @tyczj I have tried that too, i tried 76 because i noticed the string produced by android has new lines every 76th character – Hamzah Malik Aug 18 '15 at 21:12
  • Add an example, data hex dump and Base64 string. Have you compared them at all visually? Are the lengths the same? Are they being URL encoded on transfer? – zaph Aug 18 '15 at 21:13
  • @zaph ok. What is a hex dump? Visually they are nothing alike. I checked visual alikeness before transferring, from the output of println. Ill add example now – Hamzah Malik Aug 18 '15 at 21:17
  • Hmm??? Googling [hex dump](https://en.wikipedia.org/wiki/Hex_dump). That was not to hard. – zaph Aug 18 '15 at 21:19
  • @zaph ive added exmaples – Hamzah Malik Aug 18 '15 at 21:32
  • 1
    @HamzahMalik I think you need update your question because problem is in PNG binary data you send to base64 encoder – John Tracid Aug 18 '15 at 22:00
  • Actually close this question and ask another just about iOS PNG encoding, a new title will help. Consider that the question about Base64 encoding is answered, that there is no problen with it. – zaph Aug 18 '15 at 22:17

1 Answers1

4

The Base64 provoided are from different PNG encodings. The headers are different, Android has a "sBIT" chunk and iOS has a "sRGB" chunk.

Thus the problem is not Base64 but the representatins prpovided by the two systems.

Decoded portions

Android:
âPNG

IHDR††≠zsBIT€·O‡ÑIDAT

iOS:
âPNG

IHDR»»≠XÆûsRGBÆŒÈiDOTd(ddp`ùıºIDAT

zaph
  • 111,848
  • 21
  • 189
  • 228