https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
v

vanniktech

11/23/2019, 6:12 PM
Has anyone been able to convert a file from an iOS system into a KotlinByteArray? I want to upload an image using ktor and ByteArray is the common denominator between the platforms. Android side is already working.
1
m

matej

05/19/2020, 4:30 PM
Did you ever solve this? ByteArray is a bit dangerous, isn't it? You have to read the whole image into memory in that case, and this could cause low memory warnings/errors if the image is big. I'm having the same conundrum now, but with uploading audio files, which can get even bigger.
v

vanniktech

05/19/2020, 7:50 PM
Nope I haven’t
k

Kivia Brito

05/26/2020, 10:46 PM
This is how I am converting an image(data) to KotlinByteArray in Swift.
Copy code
let swiftByteArray = [UInt8](data)
  let intArray : [Int8] = swiftByteArray.map { Int8(bitPattern: $0) }
  let kotlinByteArray: KotlinByteArray = KotlinByteArray.init(size: Int32(swiftByteArray.count))
  for (index, element) in intArray.enumerated() {
   kotlinByteArray.set(index: Int32(index), value: element)
  }
m

matej

06/23/2020, 1:06 PM
@Kivia Brito I ended up doing something similar, it works for my use case (upload) for now, but I'll have to replace it with a buffered/chunked solution eventually. A couple of hundred megabyte file is not gonna be fun to load into a byte array in its entirety.
👍 1
2 Views