[英]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 one擁有更大的字符數
所提供的Base64來自不同的PNG編碼。 標題不同,Android有一個“sBIT”塊,iOS有一個“sRGB”塊。
因此問題不是Base64,而是由兩個系統提供的代表。
解碼部分
安卓:
APNG
IHDR††≠zsBIT€·O2‡ÑIDAT
iOS版:
APNG
IHDR»»≠XÆûsRGBÆŒÈiDOTd(ddp`ùıºIDAT
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.