Created
March 30, 2024 11:02
-
-
Save oianmol/0089095c9327190cb586261224dcc94e to your computer and use it in GitHub Desktop.
Image bytes to file kmp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(UnsafeNumber::class, ExperimentalForeignApi::class) | |
private fun ByteArray.toNSData(): NSData = NSMutableData().apply { | |
if (isEmpty()) return@apply | |
[email protected] { | |
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