1

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?

Peter
  • 1,860
  • 2
  • 18
  • 47
  • 1
    Encoding: `options: .lineLength64Characters)` and decoding: `options: .init(rawValue: 0)` Might want to test with `.ignoreUnknownCharacters` instead of `init(rawValue: 0)`? – Larme Jan 25 '21 at 14:58
  • 1
    @Larme it worked with `ignoreUnknownCharacters ` – Peter Jan 25 '21 at 15:02

1 Answers1

2

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).

Larme
  • 24,190
  • 6
  • 51
  • 81