Skip to content

Instantly share code, notes, and snippets.

@oianmol
Created March 30, 2024 11:02
Show Gist options
  • Save oianmol/0089095c9327190cb586261224dcc94e to your computer and use it in GitHub Desktop.
Save oianmol/0089095c9327190cb586261224dcc94e to your computer and use it in GitHub Desktop.
Image bytes to file kmp
@OptIn(UnsafeNumber::class, ExperimentalForeignApi::class)
private fun ByteArray.toNSData(): NSData = NSMutableData().apply {
if (isEmpty()) return@apply
this@toNSData.usePinned {
appendBytes(it.addressOf(0), size.convert())
}
}
@OptIn(UnsafeNumber::class, ExperimentalForeignApi::class)
private fun NSData.toByteArray(): ByteArray {
val result = ByteArray(length.toInt())
if (result.isEmpty()) return result
result.usePinned {
memcpy(it.addressOf(0), bytes, length)
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment