Setting A CGContext Transparent Background

IphoneGraphicsBackgroundTransparentCgcontext

Iphone Problem Overview


I am still struggling with drawing a line with CGContext. I have actually go to line to draw, but now I need the background of the Rect to be transparent so the existing background shows thru. Here's my test code:

(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetAlpha(context,0.0);
    CGContextFillRect(context, rect);
    
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 5.0);
    CGContextMoveToPoint(context, 100.0,0.0);
    CGContextAddLineToPoint(context,100.0, 100.0);
    CGContextStrokePath(context);
}

Any ideas?

Iphone Solutions


Solution 1 - Iphone

After UIGraphicsGetCurrentContext() call CGContextClearRect(context,rect)

Edit: Alright, got it.

Your custom view with the line should have the following:

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

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextClearRect(context, rect);
	CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
	CGContextSetLineWidth(context, 5.0);
	CGContextMoveToPoint(context, 100.0,0.0);
	CGContextAddLineToPoint(context,100.0, 100.0);
	CGContextStrokePath(context);
}

My test used this as a very basic UIViewController:

- (void)viewDidLoad {
    [super viewDidLoad];
	UIImageView *v = [[UIImageView alloc] initWithFrame:self.view.bounds];
	[v setBackgroundColor:[UIColor redColor]];
	[self.view addSubview:v];
	TopView *t = [[TopView alloc] initWithFrame:self.view.bounds];
	[self.view addSubview:t];
	[v release];
	[t release];
}

Solution 2 - Iphone

Easy way:

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {       
        self.opaque = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);
    //your code
}

Solution 3 - Iphone

Init a context with opaque == false, Swift 3

UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale) 

opaque > A Boolean flag indicating whether the bitmap is opaque. If you know the bitmap is fully opaque, specify true to ignore the alpha channel and optimize the bitmap’s storage. Specifying false means that the bitmap must include an alpha channel to handle any partially transparent pixels.

Solution 4 - Iphone

This is what worked for me with a UIImage which had been manually added using InterfaceBuilder.

- (id)initWithCoder:(NSCoder *)aDecoder {

    if(self = [super initWithCoder:aDecoder]) {
        self.backgroundColor = [UIColor clearColor];
    }
    
    return self;
}


-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();    
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 5.0);
    CGContextMoveToPoint(context, 100.0,0.0);
    CGContextAddLineToPoint(context,100.0, 100.0);
    CGContextStrokePath(context);
}

David Kanarek's answer only works when you're manually creating your own UIImageView. If you've created a UIView and manually added it via Interface Builder then you will need a different approach like this calling the initWithCoder method instead.

Solution 5 - Iphone

I have the same problem, then I find it is. I overwrite the init Method is -(id)initWithFrame:(CGRect)rect. In this method self.background = [UIColor clearColor]; but i use this view in xib file !!! That will call the init method is

-(id)initWithCoder:(NSCoder*)aDecoder;

So overwrite all the init Method and setup BackgroundColor will work OK.

Solution 6 - Iphone

CGContextClearRect(context,rect) 

If the provided context is a window or bitmap context, Quartz effectively clears the rectangle. For other context types, Quartz fills the rectangle in a device-dependent manner. However, you should not use this function in contexts other than window or bitmap contexts.

Solution 7 - Iphone

you can create a image context with this code:

cacheContext = CGBitmapContextCreate (cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast);
CGContextSetRGBFillColor(cacheContext, 0, 0, 0, 0);
CGContextFillRect(cacheContext, (CGRect){CGPointZero, size});

the key is kCGImageAlphaPremultipliedLast.

Solution 8 - Iphone

Having trouble understanding the question here, but if you're unable to have a "background" UIView show through a "top" view into which you're drawing, one solution is topView.backgroundColor = [UIColor clearColor]; I was having (I think) this same problem and this solved it for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJim BView Question on Stackoverflow
Solution 1 - IphoneDavid KanarekView Answer on Stackoverflow
Solution 2 - IphoneZGl6YXNtView Answer on Stackoverflow
Solution 3 - Iphoneonmyway133View Answer on Stackoverflow
Solution 4 - IphonePatrick CollinsView Answer on Stackoverflow
Solution 5 - Iphoneen-菜鸟..View Answer on Stackoverflow
Solution 6 - IphoneCodrutView Answer on Stackoverflow
Solution 7 - IphoneflukeView Answer on Stackoverflow
Solution 8 - IphoneoflannabhraView Answer on Stackoverflow