CGContextClip() question


#1

Hi everyone,

I’m trying to solve the gold challenge and I started with a step-by-step approach.

That is I want :

  • to display the logo (done);
  • to clip the logo (where my problem is for now);
  • to add the shadow (didn’t try);
  • to add the gradient (didn’t try);

I’m aware that those steps do not “match” the sequencial core graphics code in the drawRect: method but I want to understand what I’m doing (add a new “feature” when the previous one is understood).

I only use iPhone simulator for testing.

Here’s the code of my LogoView drawRect:

- (void)drawRect:(CGRect)rect
{
    CGPoint imageOrigin;
    imageOrigin.x = 0;
    imageOrigin.y = 0;
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(ctx, [self bounds]);
    CGContextClip(ctx);
    
    [image drawAtPoint:imageOrigin];
}

All I can get is the logo’s hat to be surrounded by a black zone that should be transparent…
I spent hours (really) to figure it out and I even tried the solution proposed by Big Nerd Ranch. It gave exactly the same result regarding clipping.

Can someone explain me a bit about CGContextClip() ?

Thanks in advance.


#2

OK my mistake.

In the initWithFrame: method of my LogoView.m I forgot to add [self setBackgroundColor:[UIColor clearColor]]; .

Sorry for that stupid topic, I should have read the chapter more carefully.