I have this code to convert UIImage to base64EncodedString and from base64EncodedString back to UIImage on both iOS and Android app.
The problem is when i convert UIImage to base64EncodedString and send to my android device it works,
also when i receive base64EncodedString from android device to IOS this function imageFromBase64 will decode it to UIImage without issue.
But when convert UIImage to base64EncodedString in IOS function and try to decode it in IOS using this function imageFromBase64 it will return nil.
I have below code to convert image to base64EncodedString
if let image = info[.originalImage] as? UIImage {
let thumb1 = image.resized(withPercentage: 0.3)
if let imageData = thumb1?.jpeg(.medium) {
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
tryEmitMessage(message: strBase64)
}
}
my android example to convert bitmap to base64EncodedString
public static byte[] bitmapToByte(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
byte[] bm = BitmapUtils.bitmapToByte(bitmap_image);
String strBase64 = Base64.encodeToString(bm, Base64.NO_WRAP);
Bellow function is what is use to decode base64EncodedString to UIImage
func imageFromBase64 (base64:String) -> UIImage {
let imageData = Data.init(base64Encoded: base64, options: .init(rawValue: 0))
let image = UIImage(data: imageData!)
return image!
}
let message = "base64 string"
dataView.uiimage_image = imageFromBase64(base64: message)
my android example to convert base64EncodedString to bitmap
byte[] b = Base64.decode("base64 string", Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(b,0,b.length);
Please any idea how to encode and decode base64 image in iOS swift?