CGContext transparent background setting

I am still struggling with line drawing with CGContext. I actually go to the line to draw, but now I need the Rect background to be transparent to show the existing background. Here is 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?

+46
iphone background transparent graphics cgcontext
Jan 24
source share
8 answers

After UIGraphicsGetCurrentContext() call CGContextClearRect(context,rect)

Edit: Good, got it.

The custom view in 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 simple 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]; } 
+70
Jan 24 '10 at 1:45
source share

A simple 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 } 
+40
Jan 28 '12 at 14:17
source share

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

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

So rewrite all init. The BackgroundColor method and setting will work fine.

+3
Jun 22 '12 at 8:24
source share
 CGContextClearRect(context,rect) 

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

+2
Sep 26 2018-11-11T00:
source share

This is what worked for me with UIImage, which was 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 Canarek's answer only works when you manually create your own UIImageView. If you created a UIView and manually added it through Interface Builder, you will need a different approach, for example, to call the initWithCoder method.

+2
Nov 22 '14 at 10:18
source share

you can create an 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.

+1
04 Oct '13 at 9:33
source share

It was not possible to understand the question here, but if you cannot get the "background" view of the UIView through the "top" view you are drawing into, one of the solutions is topView.backgroundColor = [UIColor clearColor]; I had (I think) the same problem, and this solved it for me.

0
Jul 29 2018-11-17T00:
source share

Initiate 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 that the bitmap is completely opaque, specify true to ignore the alpha channel and optimize the storage of bitmap images. Setting false means that the bitmap must include an alpha channel for processing partially transparent pixels.

0
Jun 22 '17 at 10:37
source share



All Articles