Fill color on specific portion of image?

前端 未结 4 1462
北海茫月
北海茫月 2021-01-30 18:32

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

EX:

\"enter

相关标签:
4条回答
  • 2021-01-30 18:52

    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

    0 讨论(0)
  • 2021-01-30 18:54

    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

    0 讨论(0)
  • 2021-01-30 18:58

    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.

    0 讨论(0)
  • 2021-01-30 18:59

    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.

    0 讨论(0)
提交回复
热议问题