Base64 encoding in Swift will not decode in Android

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

  • Disabling swiping gestures in mobile chrome browser
  • Android ignores maximum-scale when using fixed-width viewport meta-tag
  • How to create some kind of Splash screen/Launching screen, which disappears after App loaded? (React Native)
  • How to handle multiple markers on Google Maps with same location?
  • iOS equivalent to Android Service?
  • display connection error messages in ionic framework
  • 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

    Solutions Collect From Internet About “Base64 encoding in Swift will not decode in Android”

    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