How to change size of CGContextRef - iphone

How can I increase size of CGContextRef after its creation.
if(UIGraphicsBeginImageContextWithOptions != NULL) {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, 0);
}
else {
UIGraphicsBeginImageContext(CGSizeMake(width, height));
}
CGContextRef ctr = UIGraphicsGetCurrentContext();
Can i change size of ctr?

Try doing it without the if-else conditions. Create a CGContext using that last line of code only.
Here's a simple example
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 100,100);
CGContextAddLineToPoint(context, 200, 200);
CGContextStrokePath(context);
}

Related

Draw line between two different point

I want to draw line between two points. Here is my code. but context memory is 0.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 300, 400);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
You need to call your code within a UIView subclass in the method drawRect:.
UIGraphicsGetCurrentContext() will get NULL (0) when your are not in a drawing context.
You might also check the docs.
Here is a very useful tutorial for your need.
http://trailsinthesand.com/exploring-iphone-graphics-part-1/
Go through it once..!

Draw multiple lines Quartz 2D

I want to draw 15 horizontal lines, with 20 points space between them. I can't understand why this code doesn't work.
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2.0);
for (int i=20;i+=20;i<=20*15) {
CGContextBeginPath(context);
CGContextMoveToPoint(context, 10, i);
CGContextAddLineToPoint(context, 310, i);
CGContextStrokePath(context);
}
}
Thank you!
Yes, the for loop should be:
for (int i=20; i<=20*15; i+=20) {
...
}

Cant draw a line in iphone

I want to draw a line of 2 pixels..I have written the following code but it does not draw any line or anything on the view..Plzz help me regarding this or tell me the error in this code...In code pointToBeShown is CGPoint.....
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColor(context, currentColor.CGColor);
pointToBeShown.x = 30;
pointToBeShown.y = 449;
CGContextMoveToPoint(context, pointToBeShown.x, pointToBeShown.y);
CGContextAddLineToPoint(context, (pointToBeShown.x + 1),( pointToBeShown.y + 1));
CGContextStrokePath(context);
}
Are you not getting any warning?
Replace the line
CGContextSetStrokeColor(context, currentColor.CGColor);
with
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
Update:
Update as you mentioned in the comments
CGContextRef context = UIGraphicsGetCurrentContext();
[currentColor set];
CGContextSetLineWidth(context, 2.0);
CGContextFillEllipseInRect(context, CGRectMake(pointToBeShown.x, pointToBeShown.y, 10, 10));

Draw curved arrow between two points using Quartz Core in iPhone

I want to draw a curved line with arrow between two points using the Quartz Core framework in an iPhone Application. How to do that or any what classes are available to do that ?
You can change the co-ordinates as you want
- (void)drawRect:(CGRect)rect {
//DRAW CURVE
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 100, 100);
CGContextAddArcToPoint(context, 100,200, 300,200, 100);
CGContextStrokePath(context);
//DRAW LINE
CGContextSetLineWidth(context, 2.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 300, 400);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}

my colour bitmap is only black and white in drawRect (iPhone)

I want to preserve previous drawing in drawRect (the system clears it down each time) and it seems that the way to go is to use a bitmap context and sure enough, this works except that now my colour is gone leaving only black and white instead.
Here is my "static" context
-(id)initWithCoder:(NSCoder *)aDecoder {
...
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
contextRef = CGBitmapContextCreate(NULL,
320,
640,
8,
320*4,
colorSpace,
kCGImageAlphaPremultipliedLast);
...
}
and here is my replacement context:
-(void)drawRect:(CGRect)rect {
myDrawRoutine();
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, rect, CGBitmapContextCreateImage(contextRef));
}
And this is the draw routine's guts:
...
// CGContextRef context = UIGraphicsGetCurrentContext();
CGContextRef context = contextRef;
if( !(context) )
return;
CGContextSetFillColor(context, CGColorGetComponents([co CGColor]));
CGContextSetStrokeColor(context, CGColorGetComponents([co CGColor]));
CGContextFillEllipseInRect(context, CGRectMake((*i)->x,
(*i)->y,
(*i)->diam,
(*i)->diam));
...
}
Thanks tonclon
That has made a hell of an increase in speed :)
But it still draws in black and white :(
Here is the above code + modifications...
Here is my "static context":
-(id)initWithCoder:(NSCoder *)aDecoder {
...
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
contextRef = CGBitmapContextCreate(NULL,
320,
640,
8,
320*4,
colorSpace,
kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
cglayer = CGLayerCreateWithContext(contextRef, CGSizeMake(320.0f, 640.0f), NULL);
contextRef = CGLayerGetContext(cglayer);
...
}
and here is my "replacement context" i.e. the cglayer is used instead (the context is no longer replaced).
-(void)drawRect:(CGRect)rect {
for(std::vector<Body*>::iterator i = bodyVec.begin(); i < bodyVec.end(); ++i)
move(i);
CGContextRef context = UIGraphicsGetCurrentContext();
// CGContextDrawImage(context, rect, CGBitmapContextCreateImage(contextRef));
CGContextDrawLayerInRect(context, rect, cglayer);
}
(The "draw routine's guts" remain the same.)
At last! Thanks to this thread
Extracting rgb from UIColor
I got the answer - use CGContextSetRGBFillColor instead and then feed it with the RGB from CGColorGetComponents indirectly.
CGColorRef color = [co CGColor];
int numComponents = CGColorGetNumberOfComponents(color);
CGFloat red;
CGFloat green;
CGFloat blue;
CGFloat alpha;
if (numComponents == 4)
{
const CGFloat *components = CGColorGetComponents(color);
red = components[0];
green = components[1];
blue = components[2];
alpha = components[3];
}
// CGContextSetFillColor(context, CGColorGetComponents([co CGColor]));
// CGContextSetStrokeColor(context, CGColorGetComponents([co CGColor]));
CGContextSetRGBFillColor(context, red, green, blue, alpha);
CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
CGContextFillEllipseInRect(context, CGRectMake((*i)->x,
(*i)->y,
(*i)->diam,
(*i)->diam));
}