简体   繁体   English

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

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

Images encoded in iOS WILL decode in iOS 在iOS中编码的图像将在iOS中解码

In Android, I am using the following to encode and decode 在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;
}

And the following on the iOS side 以下是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
}

} }

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 示例:以此图像为例https://pbs.twimg.com/profile_images/522909800191901697/FHCGSQg0.png

On Android it encodes to http://pastebin.com/D41Ldjis 在Android上,它编码为http://pastebin.com/D41Ldjis

And on iOS to http://pastebin.com/fEUZSJvF 在iOS上访问http://pastebin.com/fEUZSJvF

iOS one has a much larger character count iOS one拥有更大的字符数

The Base64 provoided are from different PNG encodings. 所提供的Base64来自不同的PNG编码。 The headers are different, Android has a "sBIT" chunk and iOS has a "sRGB" chunk. 标题不同,Android有一个“sBIT”块,iOS有一个“sRGB”块。

Thus the problem is not Base64 but the representatins prpovided by the two systems. 因此问题不是Base64,而是由两个系统提供的代表。

Decoded portions 解码部分

Android: 安卓:
âPNG APNG

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

iOS: iOS版:
âPNG APNG

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM