Swift 中的 Base64 编码不会在 Android 中解码

Base64 encoding in Swift will not decode in Android

我有一个 Android 应用程序,它使用 Base64 对图像进行编码,编码后的字符串存储在服务器上。我现在正在为同一个应用程序制作一个 iOS 客户端,并且正在努力使其以相同的方式编码图像 在 android 端编码的图像将在 Swift iOS 中解码,但在 Swift 中编码的图像将不会在 Android 或此处 http://www.freeformatter.com/base64-encoder.html 中解码(生成的文件不是有效图像)

在 iOS 中编码的图像将在 iOS

中解码

在Android中,我使用下面的编码和解码

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;
}

以及 iOS 方面的以下内容

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
}

}

我愿意更改任一客户端以使其正常工作

示例: 以这张图片为例 https://pbs.twimg.com/profile_images/522909800191901697/FHCGSQg0.png

在 Android 上它编码为 http://pastebin.com/D41Ldjis

然后 iOS 到 http://pastebin.com/fEUZSJvF

iOS 一个字符数更大

提供的Base64来自不同的PNG编码。 headers 不同,Android 有一个 "sBIT" 块,而 iOS 有一个 "sRGB" 块。

因此问题不在于Base64,而在于两个系统提供的representatins。

解码部分

Android:
âPNG

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

iOS:
âPNG

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