Monday, July 16, 2012

How to create a transparent background in CGContext ?


 CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGRect a=self.frame;
    a.origin.x=0;
    a.origin.y=0;
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);    
    CGContextAddRect(context, a);
    CGContextFillPath(context);
    CGContextRestoreGState(context);



Don't forget to add the background color as clear color.



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

No comments:

Post a Comment