Fill color on specific portion of image?

I want to fill specific color on specific area of an image.


  • How do you tell what object is being touched in touchesBegan?
  • Retrieving image from Firebase Storage using Swift
  • How to create a custom UIImagePickerController with full-screen images ios
  • move a CALayer (add animation)
  • Passing through touches to UIViews underneath
  • Passing Image to another View Controller (Swift)
  • enter image description here

    In above Joker image, If touch on hair of Joker then fill specific color on hair Or touch on nose then fill specific color on nose.. etc. I hope may you understand what I am trying to say.

    After googling it’s may be achieve by use of UIBezierPath or CGContext Reference but I am very new for it, I tried to read this documentation but I do not understand (take more time) anything also I have limit of time for this Project. So I can not spend more time on it.

    Also I found that we can use Flood fill algorithm. But I don’t know how to use in my case.

    NOTE: I don’t want to divide original image (such like hair. nose, cap,…etc) because If I will do then there will be so many images in bundle so I need to handle it for both normal and retina device so this option is not helpful for me.

    So please give me your valuable suggestion and also tell me which is best for me UIBezierPath or CGContext Reference? How can I fill color on specific portion of image? and/or can we fill color under the black border of area ? Because I am new at Quartz 2D Programming.

    4 Solutions Collect From Internet About “Fill color on specific portion of image?”

    Use the Github library below. The post uses the flood fill algorithm : UIImageScanlineFloodfill

    Objective C description : ObjFloodFill

    If you want to look at detailed explanation of the algorithm : Recursion Explained with the Flood Fill Algorithm (and Zombies and Cats)

    few of the other tutorials in other languages : Lode’s Computer Graphics Tutorial : Flood Fill

    Rather than attempting to flood fill an area of a raster-based image, a better approach (and much smaller amount of data) would be to create vector images. Once you have a vector image, you can stroke the outline to draw it uncolored, or you can fill the outline to draw it colored.

    I recommend using CoreGraphics calls like CGContextStrokePath() and CGContextFillPath() to do the drawing. This will likely look better than using the flood fill because you will get nicely anti-aliased edges.

    Apple has some good documentation on how to draw with Quartz 2D. Particularly the section on Paths is useful to what you’re trying to do.

    You can Clip the context based on image Alpha transparency

    I have created a quick image with black color and alpha

    enter image description here

    Then using the below Code

    - (void)drawRect:(CGRect)rect
        CGContextRef context = UIGraphicsGetCurrentContext(); // Get the context
        CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor); // Set the fill color to be blue
        // Flip the context so that the image is not flipped
        CGContextTranslateCTM(context, 0, rect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        // Clip the context by a mask created from the image
        UIImage *image = [UIImage imageNamed:@"image.png"];
        CGImageRef cgImage = image.CGImage;
        CGContextClipToMask(context, rect, cgImage);
        // Finally fill the context with the color and mask you set earlier
        CGContextFillRect(context, rect);

    The result

    enter image description here

    This is a quick hint of what you can do. However you need now to convert your image to add alpha transparent to the parts you need to remove

    After a quick search I found these links

    How can I change the color 'white' from a UIImage to transparent

    How to make one colour transparent in UIImage

    If You create your image as SVG vector base image, it will be very light (less than png, jpg) and really easy to manage via Quartz 2D using bezier paths. Bezier paths can be filled (white at starting). UIBezierPath have a method (containsPoint:) that help define if you click inside.