John O'Reilly
03/19/2025, 3:27 PMData
instance (imageData
below) to Kotlin ByteArray
in Swift code (so it can be passed to KMP code).....I have following atm but feels like there should be cleaner way to do this?
let kotlinByteArray: KotlinByteArray = KotlinByteArray.init(size: Int32(imageData.count))
for (index, element) in imageData.enumerated() {
kotlinByteArray.set(index: Int32(index), value: Int8(element))
}
hfhbd
03/19/2025, 4:16 PMJohn O'Reilly
03/19/2025, 4:32 PMhfhbd
03/19/2025, 4:51 PMrusshwolf
03/19/2025, 4:55 PMJohn O'Reilly
03/19/2025, 4:57 PMrusshwolf
03/19/2025, 4:59 PMrusshwolf
03/19/2025, 5:00 PMJohn O'Reilly
03/19/2025, 5:01 PMJohn O'Reilly
03/19/2025, 5:01 PMKotlinByteArray
in Swift override)John O'Reilly
03/19/2025, 5:02 PMJason
03/19/2025, 5:02 PM@OptIn(ExperimentalForeignApi::class)
public fun NSData.toByteArray(): ByteArray {
return ByteArray(length.toInt()).apply {
usePinned {
memcpy(it.addressOf(0), bytes, length)
}
}
}
russhwolf
03/19/2025, 5:02 PMrusshwolf
03/19/2025, 5:03 PMJohn O'Reilly
03/19/2025, 5:03 PMrusshwolf
03/19/2025, 5:03 PMJason
03/19/2025, 5:04 PM@OptIn(ExperimentalForeignApi::class, BetaInteropApi::class)
public fun ByteArray.toNSData(): NSData = memScoped {
NSData.create(bytes = allocArrayOf(this@toNSData), length = this@toNSData.size.toULong())
}
John O'Reilly
03/19/2025, 5:06 PMData
as opposed to NSData
.....but can probably convert between themrusshwolf
03/19/2025, 5:06 PMJohn O'Reilly
03/19/2025, 6:05 PM