Fill color on specific part of image in ios Swift 3.0

I am working on a color application. In which the application has an image, and we have to paint it with the selected color.

I am trying a lot to complete the application. But still, don't get the perfect way to fill color in a specific part of the image.

I am trying to get the color of a pixel using this code.

func getPixelColor(pos: CGPoint) -> UIColor {

        let pixelData = self.cgImage!.dataProvider!.data
        let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

        let pixelInfo: Int = ((Int(self.size.width) * Int(pos.y)) + Int(pos.x)) * 4

        let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
        let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
        let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
        let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)

        return UIColor(red: r, green: g, blue: b, alpha: a)
}

      

The higher the running code, the more it determines the color of the pixel.

But after the code above, try to fill the color on a specific part of the image touch.

Flood Algo is also used, but it does not completely fill the specific part.

Tried Masking method to fill a specific part of the image.

Picture

I am using below code to fill color on touch pixel as shown in picture above. You see a red color that shows the location of the touch and changes the color of that pixel to red.

 UIGraphicsBeginImageContextWithOptions((rect?.size)!, false, (images?.scale)!)
        let c = UIGraphicsGetCurrentContext()
        images?.draw(in: rect!)

        c!.setFillColor(UIColor.red.cgColor)
        c!.setBlendMode(CGBlendMode.normal)


        c!.fill(CGRect(x: (location?.x)!, y: (location?.y)!, width: 10, height:10))

         let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        img.image = result

      

Anyone has a solution for it. However, I can only fill the color with a pixel of the touch area, but we have to color on a specific part of the touch with the selected color. Also, be careful that the color must fill the inner border outside or outside the colored border.

if anyone has a sample code please share it.

Thank.

+3


source to share


1 answer


Use this project: https://github.com/Chintan-Dave/UIImageScanlineFloodfill

Here we have set the limit button. so it defaults to 10. Then set the image size to 300x300 for all iPhones and 600x600 for iPads. it works great for me. Deliver superior image quality.



Hope this answer helps you.

0


source







All Articles