加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 05:00:43 所属栏目:百科 来源:网络整理
导读:我有一个 Android应用程序,它使用Base64编码图像,编码字符串存储在服务器上.我现在正在为同一个应用程序制作iOS客户端,并且正在努力使其以相同的方式对图像进行编码 在Android端编码的图像将在Swift iOS中解码,但在Swift中编码的图像将不会在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:
aPNG

IHDR??≠?zsBIT

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读