https://kotlinlang.org logo
#compose-ios
Title
# compose-ios
a

Aakansha Arya

02/22/2024, 11:43 AM
Is there a way to get ByteArray/UIImage from Compose ImageBitmap for KMM iOS?
👀 2
a

Andrew Watson

02/26/2024, 10:34 PM
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
4 Views