Is there a way to get ByteArray/UIImage from Compo...
# compose-ios
a
Is there a way to get ByteArray/UIImage from Compose ImageBitmap for KMM iOS?
👀 2
a
Yes, this is how you can convert an ImageBitmap to a ByteArray or UIImage:
Copy code
fun ImageBitmap.toUIImage(): UIImage {
    return UIImage(data = this.toNSData())
}

fun ImageBitmap.toNSData(): NSData {
    val bytes = Image.makeFromBitmap(this.asSkiaBitmap()).encodeToData()?.bytes
    return bytes?.toNSData()
        ?: throw IllegalArgumentException("Error converting image for iOS consumption, please investigate")
}

@OptIn(
    ExperimentalForeignApi::class
)
fun ByteArray.toNSData(): NSData = usePinned {
    NSData.create(bytes = it.addressOf(0), this.size.convert())
}
👍 3