I’m trying to perform floodfill on UIImage that has opacity. I tried implementing a 4-Way floodfill with CG(http://en.wikipedia.org/wiki/Flood_fill) but reading the pixel colours and colouring pixels makes it too slow. It takes over a minute to fill a 300x100px area. I need a function like:
-(UIImage*)floodFillOnImage:(UIImage*)image fromPoint:(CGPoint)start;
that is fast enough.
Does anybody have a working implementation of or have an idea for a flood fill algorithm in Objective-C that works with UIImages? I’ve seen such a paint bucket tool in some iOS drawing apps.
If it takes over a minute to flood-fill a 300×100 image, your implementation of getting and setting pixel values is probably extremely inefficient. If you draw your image into a bitmap context and then access (and modify) the pixel data directly, it should be a lot faster.
The basic approach would be to allocate a memory buffer (a C array of 4 (rgba) × width × height bytes), pass that to
CGBitmapContextCreate, draw your source image into the context (e.g. usingCGContextDrawImage), and then operate directly on the buffer (the pixel values). When you’re done, you can create an image from your context usingCGBitmapContextCreateImage.