Hey!! i need your help: i have a string with `data...
# compose-ios
e
Hey!! i need your help: i have a string with
data:image/png;base64
so i created this fun :
Copy code
fun String.cleanupImageString(): String {
    return this.replace("data:image/png;base64,", "")
        .replace("data:image/jpeg;base64,", "")
}
the i used this fun to convert to byteArray encoded to Image and convert to ImageBitmap:
Copy code
@OptIn(ExperimentalEncodingApi::class)
actual fun String.decodeThumbnailImage(): ImageBitmap? {
    return try {
        val byteArray = Base64.decode(cleanupImageString())
        val skiaImage = Image.makeFromEncoded(byteArray)
        skiaImage.toComposeImageBitmap()
    } catch (e: Exception) {
        null
    }
}
but its returning null. Any idea? or another approach? thanks
c
Do you get an exception? Debug it or add a logging line in the catch block to see what kind of Exception you might get.
e
yea! let me catch the exception
kotlin.IllegalArgumentException: Invalid symbol '
'(12) at index 76
maybe its related to this method:
Copy code
fun String.cleanupImageString(): String {
    return this.replace("data:image/png;base64,", "")
        .replace("data:image/jpeg;base64,", "")
}
c
My guess would be, based on the name of these methods, is that you are passing a decoded base64 byteArray to the
makeFromEncoded
method, which does not seem to be the right way to do this
Copy code
@OptIn(ExperimentalEncodingApi::class)
actual fun String.decodeThumbnailImage(): ImageBitmap? {
    return try {
        val byteArray = cleanupImageString().toByteArray()
        val skiaImage = Image.makeFromEncoded(byteArray)
        skiaImage.toComposeImageBitmap()
    } catch (e: Exception) {
        null
    }
}
I would try something like this
e
let me see
c
Or check the documentation of
Image.makeFromEncoded
so you don’t need to guess what it expects 😅
⬆️ 2
😅 1
e
Copy code
fun makeFromEncoded(bytes: ByteArray): Image {
    Stats.onNativeCall()
    val ptr = interopScope {
        _nMakeFromEncoded(toInterop(bytes), bytes.size)
    }
    require(ptr != NullPointer) { "Failed to Image::makeFromEncoded" }
    return Image(ptr)
}
i resolved! was something related to an extra spacing in the string
thanks!