iosswiftuiimagecgimage

How to change color for point in UIImage?


I have a code that changes the color of a certain point in an image to transparent.

func processByPixel(in image: UIImage, byPoint: CGPoint) -> UIImage? {
    guard let inputCGImage = image.cgImage else { print("unable to get cgImage"); return nil }
    let colorSpace       = CGColorSpaceCreateDeviceRGB()
    let width            = inputCGImage.width
    let height           = inputCGImage.height
    let bytesPerPixel    = 4
    let bitsPerComponent = 8
    let bytesPerRow      = bytesPerPixel * width
    let bitmapInfo       = RGBA32.bitmapInfo


    guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else {
        print("Cannot create context!"); return nil
    }
    context.draw(inputCGImage, in: CGRect(x: 0, y: 0, width: width, height: height))

    guard let buffer = context.data else { print("Cannot get context data!"); return nil }

    let pixelBuffer = buffer.bindMemory(to: RGBA32.self, capacity: width * height)

    let offset = Int(byPoint.x) * width + Int(byPoint.y)
    pixelBuffer[offset] = .transparent

    let outputCGImage = context.makeImage()!
    let outputImage = UIImage(cgImage: outputCGImage, scale: image.scale, orientation: image.imageOrientation)

    return outputImage
}

By tapping on the picture, I calculate the point on which it was clicked and pass it to this function. The problem is that the color changes slightly with the offset. For example I will pass CGPoint(x: 0, y:0) but change color to 0, 30

I think that the offset variable is not calculated correctly


Solution

  • Calculate the offset like this:

        let offset = Int(byPoint.y) * width + Int(byPoint.x)
    
    

    It is the number of vertical lines times line length plus x.