I am working on color App. In which, App having the image and we have to color it with selected color.
I try many things to achieve to complete the app. But, yet not get perfect way to fill color in particular portion of the image.
I try to get the pixel color 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)
}
Above code working, it detects the pixel color.
But after above code, try to fill color on particular touch portion of the image.
Also used the Flood Algo but it does not fill the particular portion fully.
Tried Masking method to fill a particular portion of the Image.
I use below code to fill color on touch pixel as shown in above image, You see the red color which shows the touch location and change that pixel color to red color.
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. Yet, I am able to fill color only on touch area pixel but, we have to do color on particular touch portion with selected color.Also be care that color should fill inside boundary not on or outside the border color.
if anyone having a sample code, Please share it.
Thank you.
Use this Project: https://github.com/Chintan-Dave/UIImageScanlineFloodfill
Here we have set limit button. so put by default it to 10. Then put image size of 300x300 for all iPhone and 600x600 for iPad. it works perfectly for me. Keep your image quality perfect.
Hope this answer helps you.