在斯威夫特Base64编码不会去code Android中 [英] Base64 encoding in Swift will not decode in Android
问题描述
我有一个Android应用程序,它使用的Base64为en code图像,以及连接codeD字符串存储在服务器上。我现在正在做的的iOS客户端相同的应用程序,并正在努力使之恩code图像以同样的方式 对android端连接codeD映像将取消code在斯威夫特的iOS,但图像连接codeD斯威夫特不会去code在Android或这里的 http://www.freeformatter.com/base64-en$c$cr.html (生成的文件ISN T A有效的图像)
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)
在iOS的EN codeD映像将取消code中的iOS
Images encoded in iOS WILL decode in iOS
在Android的,我现在用的是以下为en code和德code
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;
}
而在iOS的一面以下
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
例: 借此形象的比方 https://pbs.twimg.com/profile_images/522909800191901697/FHCGSQg0.png
Example: take this image for example https://pbs.twimg.com/profile_images/522909800191901697/FHCGSQg0.png
在Android上它连接codeS到 http://pastebin.com/D41Ldjis
On Android it encodes to http://pastebin.com/D41Ldjis
和iOS上,以 http://pastebin.com/fEUZSJvF
的iOS 1具有大得多的字符计数
iOS one has a much larger character count
推荐答案
中的Base64 provoided来自不同PNG编码。标头是不同的,Android有一个SBIT块和iOS拥有的sRGB大块。
The Base64 provoided are from different PNG encodings. The headers are different, Android has a "sBIT" chunk and iOS has a "sRGB" chunk.
因此,问题是没有的Base64但重新presentatins prpovided由两个系统。
Thus the problem is not Base64 but the representatins prpovided by the two systems.
德codeD部分
安卓
APNG
Android:
âPNG
IHDR††≠zsBIT€·Ø‡ÑIDAT
IHDR††≠zsBIT€·O‡ÑIDAT
的iOS:
APNG
iOS:
âPNG
IHDR»»≠XÆûsRGBÆŒÈiDOTd(ddp`ùıºIDAT
IHDR»»≠XÆûsRGBÆŒÈiDOTd(ddp`ùıºIDAT
这篇关于在斯威夫特Base64编码不会去code Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!