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
In JavaScript there are two functions respectively for decoding and encoding Base64 strings: btoa() : creates a Base64-encoded ASCII string from a "string" of binary data ("btoa" should be read as "binary to ASCII"). atob() : decodes a Base64-encoded string ("atob" should be read as "ASCII to binary").
You can encode arbitrary bytes in base64 (which is why the encoding functions don't return errors). Only decoding can fail. The whole point of Base64 encoding is to take arbitrary bytes and reduce them to printable ASCII characters. There is no such thing as an invalid character for encoding, only for decoding.
To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag. As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.
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(ddp`ùıºIDAT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With