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?
After
UIGraphicsGetCurrentContext()
callCGContextClearRect(context,rect)
Edit: Alright, got it.
Your custom view with the line should have the following:
My test used this as a very basic UIViewController:
Easy way:
you can create a image context with this code:
the key is kCGImageAlphaPremultipliedLast.
I have the same problem, then I find it is. I overwrite the init Method is
-(id)initWithFrame:(CGRect)rect.
In this methodself.background = [UIColor clearColor];
but i use this view in xib file !!! That will call the init method isSo overwrite all the init Method and setup BackgroundColor will work OK.
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.
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.