You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you know ImageBitmap might not be the best end result for some user in multiplatform, given its not platform independent.
I suggest adding this few lines in the readme, maybe further down the road as a function to help new users obtain a Base64 String they can use to easily store their signature.
commonMain: expect fun ImageBitmap.toBase64(): String
androidMain:
actual fun ImageBitmap.toBase64(): String {
val bitmap = this.asAndroidBitmap()
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)
val byteArray = byteArrayOutputStream.toByteArray()
return Base64.encodeToString(byteArray, Base64.NO_WRAP)
}
iosMain:
@OptIn(ExperimentalForeignApi::class)
fun ImageBitmap.toUIImage(): UIImage? {
val width = this.width
val height = this.height
val buffer = IntArray(width * height)
this.readPixels(buffer)
val colorSpace = CGColorSpaceCreateDeviceRGB()
val context = CGBitmapContextCreate(
data = buffer.refTo(0),
width = width.toULong(),
height = height.toULong(),
bitsPerComponent = 8u,
bytesPerRow = (4 * width).toULong(),
space = colorSpace,
bitmapInfo = CGImageAlphaInfo.kCGImageAlphaPremultipliedLast.value
)
val cgImage = CGBitmapContextCreateImage(context)
return cgImage?.let { UIImage.imageWithCGImage(it) }
}
actual fun ImageBitmap.toBase64(): String {
val uiImage = this.toUIImage()
val jpegData = uiImage?.let { UIImageJPEGRepresentation(it, 0.5) }
?: return ""
return jpegData.base64Encoding()
}
The text was updated successfully, but these errors were encountered:
Hey @FelipeCoro , thank you for this! I'll add it to the README and, later, create some utility functions for Sain to convert ImageBitmap to a String across all platforms, including Android, iOS, JVM, WASM, and JS.
As you know ImageBitmap might not be the best end result for some user in multiplatform, given its not platform independent.
I suggest adding this few lines in the readme, maybe further down the road as a function to help new users obtain a Base64 String they can use to easily store their signature.
commonMain:
expect fun ImageBitmap.toBase64(): String
androidMain:
iosMain:
The text was updated successfully, but these errors were encountered: