I have a Kotlin Multiplatform project in which users can select a profile picture from gallery. In iOS, the selected profile picture is available as a UIImage, which I then center crop and convert into a ByteArray to send to AWS API Gateway with Content-type image/png. Here is the kotlin code for the UIImage -> ByteArray process in iOS:
val croppedUIImage = uiImage.CGImage?.let { cgImage ->
val width = CGImageGetWidth(cgImage).toDouble()
val height = CGImageGetHeight(cgImage).toDouble()
val squareSize = minOf(width, height)
val x = (width - squareSize) / 2
val y = (height - squareSize) / 2
val rect = CGRectMake(x, y, squareSize, squareSize)
UIImage(CGImageCreateWithImageInRect(image = cgImage, rect = rect))
} ?: throw NullPointerException("Null CGImage")
val nsData = UIImagePNGRepresentation(croppedUIImage)
?: throw CharacterCodingException("Can't represent UIImage as PNG")
return ByteArray(nsData.length.toInt()).apply {
usePinned {
memcpy(it.addressOf(0), nsData.bytes, nsData.length)
}
}
This ByteArray is then sent through Ktor to API Gateway as:
val response: HttpResponse = client.put(
"$AWS_IMAGES_API_URL/$filename.png"
) {
contentType(ContentType.Image.PNG)
header("X-Api-Key", AWS_IMAGES_API_KEY)
setBody(
ByteArrayContent(
bytes = pfp,
contentType = ContentType.Image.PNG
)
)
}
However, I get a "Bad content-type format: text; charset=utf-8" when executing this code. The API Gateway seems to be working fine, since everything runs smoothly on the Android side. Any help on this issue is appreciated, thanks
Found out the problem: turns out, the very first photo I am selecting to crop and send from Android Studio's emulator gallery seems to be bugged, and even displays upside down when cropped. Selecting any other photo seems to work fine and the cropped version gets sent correctly :/