Unable to decode base64string to uiimage in iOS swift but working fine in android [duplicate]

只谈情不闲聊 提交于 2021-01-29 09:52:02

问题


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?


回答1:


You do:

let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)

and

let imageData = Data(base64Encoded: base64, options: .init(rawValue: 0))

So you added an option for the encoding, but not for the decoding? Is the default" decoding working then? No need to specify that there was a perticular option set?

Let's see the doc of lineLength64Characters:

Set the maximum line length to 64 characters, after which a line ending is inserted.

Let's the the doc of available Data.Base64DecodingOptions, especially .ignoreUnknownCharacters:

Modify the decoding algorithm so that it ignores unknown non-Base-64 bytes, including line ending characters.

See?

Use .ignoreUnknownCharacters while decoding instead of .init(rawValue:0) (which by the way could be simplified with [], or simply not putting the parameter since [] is its default value).



来源:https://stackoverflow.com/questions/65887113/unable-to-decode-base64string-to-uiimage-in-ios-swift-but-working-fine-in-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!