Sunday, September 25, 2022
HomeiOS Developmentios - Methods to change shade for level in UIImage?

ios – Methods to change shade for level in UIImage?


I’ve a code that adjustments the colour of a sure level in a picture to clear.

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


    guard let context = CGContext(information: nil, width: width, top: top, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, house: colorSpace, bitmapInfo: bitmapInfo) else {
        print("Can not create context!"); return nil
    }
    context.draw(inputCGImage, in: CGRect(x: 0, y: 0, width: width, top: top))

    guard let buffer = context.information else { print("Can not get context information!"); return nil }

    let pixelBuffer = buffer.bindMemory(to: RGBA32.self, capability: width * top)

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

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

    return outputImage
}

By tapping on the image, I calculate the purpose on which it was clicked and go it to this operate.
The issue is that the colour adjustments barely with the offset.
For instance I’ll go CGPoint(x: 0, y:0) however change shade to 0, 30

I believe that the offset variable shouldn’t be calculated accurately

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

3 + 13 =

Most Popular

Recent Comments