Iphone graphics - iphone

can anyone give me simple code to create a line in iphone using drawrect?

Go through the blog tutorial for drawing shape including line
http://howtomakeiphoneapps.com/2009/08/how-to-draw-shapes-with-core-graphics/
Edited: Below is the code for drawing line
- (void)drawRect:(CGRect)rect {
CGContextRef c = UIGraphicsGetCurrentContext();
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGContextMoveToPoint(c, 5.0f, 5.0f);
CGContextAddLineToPoint(c, 50.0f, 50.0f);
CGContextStrokePath(c);
}

Related

Multicolor line drawing

I am creating a drawing app. One of the features is multicolour line drawing. It should work like user touch up the screen and leads on it drawing a line. Colour of the line changes smoothly. Like that http://www.examples.pavelgatilov.com/Screen%20Shot%202013-09-22%20at%208.37.42%20PM.png
I tried several approaches, but haven't been lucky.
My line drawing method is below:
- (void) drawLineFrom:(CGPoint)from to:(CGPoint)to width:(CGFloat)width
{
self.drawColor = toolColor;
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 1.0f, -1.0f);
CGContextTranslateCTM(ctx, 0.0f, -self.frame.size.height);
if (drawImage != nil) {
CGRect rect = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
CGContextDrawImage(ctx, rect, drawImage.CGImage);
}
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, width);
CGContextSetStrokeColorWithColor(ctx, self.drawColor.CGColor);
CGContextMoveToPoint(ctx, from.x, from.y);
CGContextAddLineToPoint(ctx, to.x, to.y);
CGContextStrokePath(ctx);
CGContextFlush(ctx);
drawImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
drawLayer.contents = (id)drawImage.CGImage;
}
Thanks for your help
Depending on exactly what colours, how they change and what effect you want / what happens with turns in the line, you may want to look at some combination of:
CGContextDrawLinearGradient with masking to the users drawn path.
colorWithPatternImage:
CGContextDrawLinearGradient drawn behind another layer and drawing transparency into the top layer with kCGBlendModeClear

Custom Cell Selected

I am drawing a custom cell, to achieve a certain look I want. However, I want to perform a different drawing based on if the cell is select. I really do not just want the default colors.
I changed content's views background color the view in this method:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
However, it just did not appear correctly, mainly it did not take into account the accessary and just colored it until the accessory indicator. Is there a better way to accomplish this?
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
// Background
CGContextSetFillColorWithColor(context, CELL_BACKGROUND_COLOR);
CGContextMoveToPoint(context, 0.0f, 0.0f);
CGContextAddLineToPoint(context, rect.size.width, 0.0f);
CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
CGContextAddLineToPoint(context, 0.0f, rect.size.height);
CGContextClosePath(context);
CGContextFillPath(context);
// Top line
CGContextSetStrokeColorWithColor(context, CELL_TOP_LINE_COLOR);
CGContextSetLineWidth(context, CELL_LINE_WIDTH);
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextMoveToPoint(context, 0.0f, 0.0f);
CGContextAddLineToPoint(context, rect.size.width, 0.0f);
CGContextStrokePath(context);
//Bottom line
CGContextSetStrokeColorWithColor(context, CELL_BOTTOM_LINE_COLOR);
CGContextSetLineWidth(context, CELL_LINE_WIDTH);
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextMoveToPoint(context, 0.0f, rect.size.height);
CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
CGContextStrokePath(context);
}
I think you should modify your drawRect method and change how your color are set depending of the isSelected property. Changing the content's view background when the setSelected method will probably don't change anything since it will be overriding by the drawRect method.
I'm using this :
if(self.highlighted || self.selected) {
//set text color
textColor = [UIColor colorWithRed:204.0/255.0 green:255.0/255.0 blue:0 alpha:1.0];
//set background color
[[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7] set];
CGContextFillRect(context, r);
}
Also check selectionStyleproperty of your cell.
I found a solution that works great, it also draws underneath the default accessory views, like the accessory indicator. I created custom view's called BackgroundView and SelectedBackgroundView, I just use the drawRect method to create a custom drawing. It works great, and performance seems to be okay. If anyone wants to see the full code let me know.
[cell setBackgroundView:[[BackgroundView alloc] init]];
[cell setSelectedBackgroundView:[[SelectedBackgroundView alloc] init]];

Colourise CGContextRef But Preserve Alpha

I have a CGContextRef and can draw on it and specify the alpha, and if I try and use it it works perfectly. However, I am trying to colourise it (currently either red or green), but whatever blend mode I choose, the alpha is set to 1 (because I am drawing with alpha as 1). Drawing it the correct colour is not really a viable option, as I would like to be able to colour UIImages loaded from the filesystem as well, so how should I achieve this?
Edit: Example code (width and height are predefined floats, points is an array of CGPoints all of which lie inside the context and color is a UIColor with an opacity of 100%) -
UIGraphicsBeginImageContext(CGSizeMake(width,height));
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextClearRect(contextRef, CGRectMake(0.0f, 0.0f, width, height));
CGContextSetRGBStrokeColor(contextRef, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextSetRGBFillColor(contextRef, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextSetLineWidth(contextRef, lineWidth);
CGContextBeginPath(contextRef);
CGContextMoveToPoint(contextRef, points[0].x, points[0].y);
for (int i = 1; i < 4; i++) {
CGContextAddLineToPoint(contextRef, points[i].x, points[i].y);
}
CGContextAddLineToPoint(contextRef, points[0].x, points[0].y); // Encloses shape
CGContextDrawPath(contextRef, kCGPathFillStroke);
[color setFill];
CGContextBeginPath(contextRef);
CGContextSetBlendMode(contextRef, kCGBlendModeMultiply);
CGContextAddRect(contextRef, CGRectMake(0.0f, 0.0f, width, height));
CGContextDrawPath(contextRef, kCGPathFill);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Thank you in advance for your help,
jrtc27
The problem was I needed a CGContextClipToMask. This meant my code required the following:
CGContextTranslateCTM(contextRef, 0, height);
CGContextScaleCTM(contextRef, 1.0, -1.0);
to convert the coordinates and then
CGRect rect = CGRectMake(0.0f, 0.0f, width, height);
CGContextClipToMask(contextRef, rect, UIGraphicsGetImageFromCurrentImageContext().CGImage);
to fix it.

iphone uiview circle?

Newbie Q.
I wish to subclass a UIView so that it renders a circle.
How is that done in an iPhone?
In the drawRect: method do:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(ctx);
CGContextSetRGBFillColor(ctx, 1.0f, 1.0f, 1.0f, 1.0f); // white color
CGContextFillEllipseInRect(ctx, CGRectMake(10.0f, 10.0f, 100.0f, 100.0f)); // a white filled circle with a diameter of 100 pixels, centered in (60, 60)
UIGraphicsPopContext();
}

How do I determine the location of each pixel in a square drawn via Core Graphics?

I am trying to get each pixel point of a Square drawn using Core graphics.Here by making the stroke color black color,I am drawing the Square.Please give me an idea how I will get all pixel point on which this square is drawn.
- (void)drawRect:(CGRect)rect
{
CGMutablePathRef path = CGPathCreateMutable();
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGPathMoveToPoint(path, NULL, 30.0f, 30.0f);
CGPathAddLineToPoint(path, NULL, 130.0f, 30.0f);
CGPathAddLineToPoint(path, NULL, 130.0f, 130.0f);
CGPathAddLineToPoint(path, NULL, 30.0f, 130.0f);
CGPathCloseSubpath(path);
CGPathRetain(path);
CGContextSetFillColorWithColor(ctx, [UIColor clearColor].CGColor);
CGContextSetStrokeColorWithColor(ctx,[UIColor blackColor].CGColor);
CGContextSetLineWidth(ctx, 2.0);
CGContextSaveGState(ctx);
CGContextAddPath(ctx, path);
CGContextRestoreGState(ctx);
CGContextStrokePath(ctx);
CGContextRestoreGState(ctx);
CGContextRestoreGState(ctx);
[self setNeedsDisplay];
CGPathRelease(path);
}
Why are you doing all this work instead of just using CGContextFillRect() and CGContextStrokeRect()?
Your code above can be simplified to:
CGRect r = CGRectMake(30.0, 30.0, 100.0, 100.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, CGColorGetConstantColor(kCGColorClear));
CGContextFillRect(ctx, r);
CGContextSetLineWidth(ctx, 2.0);
CGContextSetStrokeColorWithColor(ctx, CGColorGetConstantColor(kCGColorBlack));
CGContextStrokeRect(ctx, r);
Also, never send -setNeedsDisplay within your -drawRect: method. You'll get an infinite loop.
I needed to do something similar for my iPhone app and while I realize it is a little late, I decided to respond anyway.
First, initialize a mutable array (points).
Next, find the minimum X and Y coordinates for your CGRect.
Do the same for the maximum.
Find the difference between the minimum and the maximum.
Now, create a for loop like the one below:
for(int x = minX; x<diffX+minX; x++){
for(int y = minY; y<diffY+minY; y++){
[points addObject:[NSValue valueWithCGPoint:CGPointMake(x,y)]];
}
}
You can now access your points through the points array.