1

I'm building an iPhone application which requires some images to be built up in a very specific way. The problem is quite difficult-to-explain so below is a diagram of what I'm trying to achieve. Basically, I want to "paint bucket" fill onto a UIImage (which will be a PNG). I assume the term "paint bucket" here will equate to a tint?

After that, I want to create a mask object (which will be updatable and may consist of multiple shapes) and then when I apply another tint/paint bucket to the original image, the areas covered by the built-up mask will be unaffected. It's basically like wrapping some tape around an object, painting it and then removing the tape. As promised, here's a diagram of what I'm after. It's important to note that although I'm using a cross here, eventually the patterns may be quite complex and will have to be inside PNGs and not created in code. Thanks for any help you might be able to give!

Masking Flow

NonatomicRetain
  • 301
  • 4
  • 14

1 Answers1

0

Create your cross (or whatever shape you want) as a black image on a white background. Apply it to your graphics context using CGContextClipToMask. Then use CGContextFillRect to fill the bounds of your context with blue. Something like this should do it:

CGRect bounds = your context bounds;
CGContextRef gc = your context;
UIImage *cross = [UIImage imageNamed:@"cross"];
CGContextSaveGState(gc); {
    CGContextClipToMask(gc, bounds, cross.CGImage);
    CGContextSetColorWithColor(gc, [UIColor blueColor].CGColor);
    CGContextFillRect(gc, bounds);
} CGContextRestoreGState(gc);
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Thanks for your response @rob . Sorry, I'm quite a newbie with these C statements (been stuck making plain old 'apps' as of late!). What is the next step(s) to actually having this rendered on the screen? Also, using this method, would it also be possible to add multiple masks (so maybe a cross, circle etc)? In addition, the line CGContextSetColorWithColor(gc, [UIColor blueColor].CGColor); is causing a compile error ("ld: symbol(s) not found for architecture i386. clang: error: linker command failed with exit code 1"). I replaced it with "CGContextSetRGBFillColor" but nothing shows. – NonatomicRetain Dec 23 '11 at 19:37
  • Also it sounds like you need to work through a Quartz (Core Graphics) tutorial or two. http://stackoverflow.com/questions/3463256/what-are-some-great-quartz-2d-drawing-tutorials – rob mayoff Dec 23 '11 at 19:41
  • Thanks for the help Rob. Yes, best to nail this myself rather than living on handouts. I was just curious about actually rendering that code on-screen. I'll work round the code to understand it more fully myself :) – NonatomicRetain Dec 23 '11 at 19:47