Ham*_*lik 7 base64 android image ios swift
我有一个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;
}
Run Code Online (Sandbox Code Playgroud)
以下是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
}
Run Code Online (Sandbox Code Playgroud)
}
我愿意改变任何一个客户端以使其工作
示例:以此图像为例 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”块。
\n\n因此,问题不在于 Base64,而在于两个系统提供的表示。
\n\n解码部分
\n\n\n\n\nAndroid:
\n
\n\xc3\xa2PNG
IHDR\xe2\x80\xa0\xe2\x80\xa0\xe2\x89\xa0\xef\xa3\xbfzsBIT\xe2\x82\xac\xc2\xb7O\xe2\x80\xa1\xc3\x91IDAT
\n\n\n\n\niOS:
\n
\n\xc3\xa2PNG
IHDR\xc2\xbb\xc2\xbb\xe2\x89\xa0X\xc3\x86\xc3\xbbsRGB\xc3\x86\xc5\x92\xc3\x88iDOTd(dd\xef\xa3\xbfp`\xc3\xb9\xc4 \xb1\xc2\xbaIDAT
\n| 归档时间: |
|
| 查看次数: |
1715 次 |
| 最近记录: |