Add color to rectangle - objective-c

I want to make a circle with a fill color. This is my code:
context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetRGBStrokeColor(context, 0, 34, 102, 1);
CGContextSetRGBFillColor(context, 135, 206, 250, 0.5);
rectangle = CGRectMake(1, 1, 500, 500);
CGContextAddArc(context, pointWhereUserClickedX, pointWhereUserClickedY, 50, 0, 2*3.14159265359, YES);
CGContextDrawPath(context, kCGPathFillStroke);
When I run it, the fill color is white, even though I have filled with a blue color. I have the same problem when I want to add a background rectangle behind two "towers" rectangles:
context = UIGraphicsGetCurrentContext();
//Background styling
CGContextSetRGBFillColor(context, 202, 255, 112, 1);
//Background setup
background = CGRectMake(1, 1, 1024, 786);
CGContextAddRect(context, background);
CGContextDrawPath(context, kCGPathFill);
//Styling
CGContextSetLineWidth(context, 2.0);
CGContextSetRGBStrokeColor(context, 0, 0, 225, 1);
CGContextSetRGBFillColor(context, 0, 0, 225, 1);
//first tower setup
firstTower = CGRectMake(20, 20, 25, 100);
CGContextAddRect(context, firstTower);
//second tower setup
secondTower = CGRectMake(20, 800, 25, 100);
CGContextAddRect(context, secondTower);
//Draw towers
CGContextDrawPath(context, kCGPathFillStroke);
When I add the background color, I still can't see any change. It's just white, so I guess it's the same problem as the circle. The second tower isn't displayed at all either.
What is wrong? Or what am I missing?

Quartz commands require colour parameters to be in the range of 0 and 1 (float). This line here (an the others similar with it):
CGContextSetRGBFillColor(context, 135, 206, 250, 0.5);
should actually be:
CGContextSetRGBFillColor(context, 135.0/255.0, 206.0/255.0, 250.0/255.0, 0.5);

Related

CGContextSetRGBFillColor always set the color to black

I have a UIView that draws a circle, this is my code.
I'm trying to set the color to "light red", but it always end up black no matter the values I put in CGContextSetRGBFillColor.
What am I doing wrong ?
- (void)drawRect:(CGRect)rect
{
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextFillEllipseInRect(contextRef, CGRectMake(20, 100, 9, 9));
CGContextSetRGBFillColor(contextRef, 255.0/255.0, 115.0/255.0, 155.0/255.0, 0.7);
CGContextStrokeEllipseInRect(contextRef, CGRectMake(20, 100, 9, 9));
CGContextSetRGBFillColor(contextRef, 255.0/255.0, 115.0/255.0, 155.0/255.0, 0.7);
}
Set the fill color before you draw the shape. And if you want to stroke, you have to set the stroke color.
- (void)drawRect:(CGRect)rect
{
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(contextRef, 255.0/255.0, 115.0/255.0, 155.0/255.0, 0.7);
CGContextFillEllipseInRect(contextRef, CGRectMake(20, 100, 9, 9));
CGContextSetRGBStrokeColor(contextRef, 255.0/255.0, 115.0/255.0, 155.0/255.0, 0.7);
CGContextStrokeEllipseInRect(contextRef, CGRectMake(20, 100, 9, 9));
}

draw a stroke around the shape, core graphic

I am drawing a shape like following :
- (void)drawRect:(CGRect)rect
{
// Draw a cross rectagle
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextMoveToPoint(context, 190, 0);
CGContextAddLineToPoint(context, 220, 0);
CGContextAddLineToPoint(context, 310, 90);
CGContextAddLineToPoint(context, 310, 120);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextFillPath(context);
CGContextRestoreGState(context);
}
I am getting a light-dark cross flag below
Now I would like to draw a stroke around the cross flag I have just drawn
What should I do to achieve this. Please advice me on this issue.
Thanks.
Surely CGContextDrawPath(context, kCGPathFillStroke); is what you're after
You can adjust the pattern and color using:
CGContextSetStrokePattern
CGContextSetStrokeColor
https://developer.apple.com/library/ios/#documentation/graphicsimaging/reference/CGContext/Reference/reference.html
So, in your case, assuming you want a plain black stroke, you'd have:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextMoveToPoint(context, 190, 0);
CGContextAddLineToPoint(context, 220, 0);
CGContextAddLineToPoint(context, 310, 90);
CGContextAddLineToPoint(context, 310, 120);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextFillPath(context);
CGContextRestoreGState(context);
}
Produces:
- (void)drawRect:(CGRect)rect
{
// Draw a cross rectagle
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
//New
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 190, 0);
CGContextAddLineToPoint(context, 220, 0);
CGContextAddLineToPoint(context, 310, 90);
CGContextAddLineToPoint(context, 310, 120);
//New
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextFillPath(context);
//New
CGContextStrokePath(context);
CGContextRestoreGState(context);
}
#WDUK :after spending hours to figure it out, I know why your above answer is not working.
The reason is when you do CGContextFillPath first, the path is eventually cleared and then you can no long do CGContextStrokePath on it again.
Therefore in order to do CGContextFillPath and CGContextStrokePath, we have to do
CGContextDrawPath(context, kCGPathFillStroke);
After trying it , I am getting the following

Weird dispatch_async memory behavior

I have the following dispatch_async code:
dispatch_async(openGLESContextQueue, ^{
[(EAGLView *)self.view setFramebuffer];
// Replace the implementation of this method to do your own custom drawing.
static const GLfloat squareVertices[] = {
-0.5f, -0.33f,
0.5f, -0.33f,
-0.5f, 0.33f,
0.5f, 0.33f,
};
static const GLubyte squareColors[] = {
127, 127, 0, 127,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
};
static float transY = 0.0f;
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
transY += 0.075f;
glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
[(EAGLView *)self.view presentFramebuffer];
});
And when in Instruments, and even though the animation is running fine, I get tons of "64 bytes malloc"s that never get freed. Anyone know why?
I was finally able to solve the problem using semaphores:
if (dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_NOW) == 0)
{
dispatch_async(openGLESContextQueue, ^{
[(EAGLView *)self.view setFramebuffer];
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
transY += 0.075f;
glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
[(EAGLView *)self.view presentFramebuffer];
dispatch_semaphore_signal(frameRenderingSemaphore);
});
}
I guess the dispatch queue was getting flooded without time to handle every opengl redraw. This way, it will only process one redraw at a time, asynchronously. Curiously, it has no side effects on the frame rate! :D
Thanks :)

Trying to use CGContextAddArc... nothing is drawn?

I am trying to draw an arc. But nothing is drawn?
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = CGRectMake(0,0,340,480);
UIView *ui = [[UIView alloc] initWithFrame:rect];
[self.view addSubview:ui];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, 50, 50, 20, 0, 30, 0);
}
When -viewDidLoad is sent, the current graphics context could be anything. Do your drawing in -drawRect:.
You have to stroke or fill the arc before its visible. You've got a path on screen but you have to stroke or fill it before draws visibly. Use these to do that..
//set the fill or stroke color
CGContextSetRGBFillColor(context, 0.5, 0.5, 0.5, 1.0);
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 1.0);
//fill or draw the path
CGContextDrawPath(context, kCGPathStroke);
CGContextDrawPath(context, kCGPathFill);

How to fill my circle(CGContextAddArc) with a different color?

I have the following but it only draws the border of a circle.
I would like to fill the circle. ??
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, 50, 50, 50, 0, 30, 0);
//set the fill or stroke color
CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0);
CGContextSetRGBStrokeColor(context, 0.5, 1, 0.5, 1.0);
//fill or draw the path
CGContextDrawPath(context, kCGPathStroke);
CGContextDrawPath(context, kCGPathFill);
You need to use CGContextFillPath to fill the path.
Remove the stroke related lines and use only fill related lines.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, 50, 50, 50, 0, 30, 0);
//set the fill or stroke color
CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0);
//fill or draw the path
CGContextDrawPath(context, kCGPathStroke);
If you want only a circle
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, 50, 50, 50, 0, 30, 0);
CGContextSetRGBStrokeColor(context, 0.5, 1, 0.5, 1.0);
CGContextDrawPath(context, kCGPathStroke);
For filling solid color it should have kCGPathFill in CGContextDrawPath(context, kCGPathFill);
For stroke with color it should have kCGPathStroke in CGContextDrawPath(context, kCGPathStroke);
Something like this:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, 50, 50, 50, 0, 30, 0);
//set the fill or stroke color
CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0);
//fill on drawn path
CGContextDrawPath(context, kCGPathFill);
CGContextAddArc(ctx, x, y, 1.0, 0, 2 * Pi, 1); // Or is it 2 * M_PI?
CGContextSetFillColorWithColor(ctx, fillColor);
CGContextSetStrokeColorWithColor(ctx, strokeColor);
CGContextDrawPath(ctx, kCGPathFillStroke);;
if you want to fill the circle you can use this
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, 50, 50, 50, 0, 30, 0);
CGContextSetRGBFillColor(context, 1, 0.5, 0.5, 1.0);
CGContextFillPath(context);
and this code below used to draw the border for circle
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddArc(context, 50, 50, 50, 0, 30, 0);
CGContextSetRGBStrokeColor(context, 0.5, 1, 0.5, 1.0);
CGContextStrokePath(context);
so, in this case you must to choose the first choice to fill the circle
Fill path:
let context = UIGraphicsGetCurrentContext()
CGContextAddArc(context, self.bounds.width/2, self.bounds.height/2, 150, 0, CGFloat(2 * M_PI), 0)
UIColor.redColor().setFill()
CGContextFillPath(context)
circle:
let context = UIGraphicsGetCurrentContext()
CGContextAddArc(context, self.bounds.width/2, self.bounds.height/2, 150, 0, CGFloat(2 * M_PI), 0)
CGContextSetLineWidth(context, 10)
UIColor.greenColor().set()
CGContextStrokePath(context)