views:

45

answers:

1

Hello

I'm a begginer developing apps for Iphone and was wondering if is possible to do the following:

I have an image .png wich is a simple draw of a figure bounded with black edges and empty areas. I wonder if there is a method or a mode to fill the closed empty areas of the image.

Its like as the pot of MSpaint filling.

I have this code:

CGContextRef ctx = UIGraphicsGetCurrentContext();
UIImage *image = [UIImage imageNamed:@"myImage.png"];
CGContextDrawImage(context, CGRectMake(0, 0, 145, 15), image.CGImage);
[image drawInRect:CGRectMake(0, 0, 145, 15)];

Now i don't know what to do to fill...

I'll be very grateful if someone can answer this question. Thank you very much!

A: 

I have an image … wich is … bounded with black edges and empty areas.

If by “empty” you mean these areas have alpha zero, simply fill the context with the desired color (filling the entire extent of the context) first, then draw the image on top. The color will show through where the image is transparent, and not where it isn't.

A closer match to what you asked for would be to draw the image using the Copy blend mode, then do the color fill using the Source Out blend mode.

Blend modes are your friend; learn them well, because you can do a lot with them.

Peter Hosey
Right, the empty areas are transparent (or alpha 0). I have an idea of what you mean, but this does all parts transparent? Imagine a boy drawn, I would like for example fill the head with one color, the body with another color ... if this is the method to be followed at least I know where to go.Thank you very much for your response! Although an example if is not very complicated would help me a lot!
Yes, it does all transparent portions. If you want the user to be able to flood-fill specific portions, you'll have to implement the fill by doing that on a pixel buffer yourself. Andy Finnell wrote a good post about the algorithm behind a flood fill: http://losingfight.com/blog/2007/08/28/how-to-implement-a-magic-wand-tool/
Peter Hosey
great! thanks again peter!