微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift中的Base64编码不会在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,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 one拥有更大的字符数

解决方法

所提供的Base64来自不同的PNG编码.标题不同,Android有一个“sBIT”块,iOS有一个“sRGB”块.

因此问题不是Base64,而是由两个系统提供的代表.

解码部分

Android:
âPNG

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

iOS:
âPNG

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐