Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent background in CoreGraphics

I am trying to get a transparent background in CG but it keeps coming out black.

I have the following code:

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        DLog(@">>> Alloc: DrawingCanvas [0x%X]", (unsigned int)self);
        [self setOpaque:NO];
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void) drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect frame = rect;
    int counter = 3;

    CGContextClearRect(context, frame);
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, frame);
}

How do I get this code to show a transparent background?

like image 739
Egil Avatar asked Feb 28 '11 21:02

Egil


1 Answers

With that setup and as long as you don't set clearsContextBeforeDrawing to NO, the background should already be transparent when your drawRect: method is called. Remove the CGContextClearRect and other drawing calls.

like image 149
Anomie Avatar answered Sep 25 '22 23:09

Anomie