hafiz
08/08/2024, 5:46 AMhafiz
08/08/2024, 5:46 AMfun ImageBitmap.toUIImage(): UIImage? {
val width = this.width
val height = this.height
val buffer = IntArray(width * height)
this.readPixels(buffer)
val colorSpace = CGColorSpaceCreateDeviceRGB()
val context = CGBitmapContextCreate(
data = buffer.refTo(0),
width = width.toULong(),
height = height.toULong(),
bitsPerComponent = 8u,
bytesPerRow = (4 * width).toULong(),
space = colorSpace,
bitmapInfo = CGImageAlphaInfo.kCGImageAlphaPremultipliedLast.value
)
val cgImage = CGBitmapContextCreateImage(context)
return cgImage?.let { UIImage.imageWithCGImage(it) }
}