I am trying to center crop a
UIImage
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" error