Fill color on specific portion of image?

前端 未结 4 1465
北海茫月
北海茫月 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

提交回复
热议问题