Justin Xu
05/13/2023, 8:38 PMUIImage
in iOS and convert the cropped version to a ByteArray
to send through HTTP, is this correct?
val croppedUIImage = bitmap.image.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)
}
}
When I send this as image/png
, I get a "Bad Content-Type format: text; charset=utf-8" errorayodele
05/14/2023, 9:44 PMJustin Xu
05/14/2023, 10:00 PMayodele
05/15/2023, 6:29 AMContentType.Multipart.FormData
Anything else, we get a bad content type errorJustin Xu
05/15/2023, 8:13 AMByteArray
in iOS, since the process of image -> bytearray -> send through HTTP works in Android. For context, I am uploading to AWS API Gateway, and sending the image file as binary data through Ktor as image/png
has been working fine for meayodele
05/15/2023, 8:47 AMByteArray
is correct. That's what we use currently.
Maybe you should try to save the result of the cropped image. I guess you tried that