How to get diagram with images as a boarder line in iOS? - iphone

I drew rectangle diagram with boarder dotted blue line. I would like to rectangle diagram with same images as a boarder line. That means i want to replace dots with images
My Code:
- (void)drawRect:(CGRect)rect
{
context =UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
CGFloat dashPattern[10] = {3.0, 2.0};
CGContextSetLineDash(context, 0, dashPattern, 2);
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextAddRect(context, self.bounds);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextClosePath(context);
CGContextStrokePath(context);
}
Can i set image instead of dots? Please give any suggestions to me. Thanks in advance.

I don't try yet, but maybe you can use:
CGContextSetStrokeColorWithColor(context, [UIColor colorWithPatternImage:myImage].CGColor);

Related

Draw a Line in UIView between 2 image views

I want to draw line between to imageviews (like linking the images..) in UIView. I have used
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 0,0); //start at this point
CGContextAddLineToPoint(context, 20, 20); //draw to this point
CGContextStrokePath(context);
But here, my context is null. I dono whether i have to import any framework or import any headers. Any suggestion or sample will be appreciated.
Thanks in advance.
The very easy way is use label with height 1.:)
UILabel *seperator=[[UILabel alloc]initWithFrame:CGRectMake(0, 53, 233, 1)];
seperator.backgroundColor=[UIColor redColor];
[loginView addSubview:seperator];
UIGraphicsBeginImageContext(image_signature.image.size);
[yourImageView.image drawInRect:CGRectMake(0, 0, yourImageView.image.size.width, yourImageView.image.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 0, 0);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 20, 20);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[yourImageView setImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
...and "yourImageView" is an imageview where you will draw the line. I suggest you to draw the line on each upper imageview, at the very bottom, it's an option.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 5.0);
CGContextMoveToPoint(context, 90, 90.0);
CGContextAddLineToPoint(context, 120.0, 150.0);
CGContextStrokePath(context);

How to fill the shapes with color

I am new to Objective-C and iOS development. I have a view with some figures drawn by core drawing. Now I want to fill those shapes with color and I don't know the path or context of shape. Does Objective-C have any function like flood-fill or put-pixel so that by having only the stroke color I can fill any shape inside my view.
-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 4.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {1.0, 0.0, 0.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 50, 0);
CGContextAddLineToPoint(context, 0,320);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetRGBFillColor(context, 0, 255, 0, 1.0);
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 100, 200);
CGContextAddLineToPoint(context, 50, 150);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(60,170,200,80);
CGContextAddEllipseInRect(context, rectangle);
CGContextStrokePath(context);
}
This will make a circle and a square with some common region. I want a particular region to fill with color when a user taps that region.
So you are looking for flood fill in Objective C.
This is my implementation of Scan Line Flood Fill Algorithm.
Github : UIImageScanlineFloodfill
If you want to learn basic of flood fill:
Lode's Computer Graphics Tutorial Flood Fill
I hope this will help you.
Put your drawing code in the question and I can give you a specific answer.
Normally you just need to set a fill color using
[[UIColor redColor] setFill];
And any shapes you draw your core graphics will be filled with that color automatically.
Try this command:-
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
Core Graphics is closely tied to UIView and its drawRect: method and has a C interface. As you point out, you always draw in the current graphics context. I can think of 2 approaches: (1) When the user taps myView, set a property like myView.pointTapped, and in drawRect: find the shape to fill, set a fill color, and fill the closed path using GGGeometry methods.
There is an objective-C alternative: (2) Take a look at UIBezierPath. It's not as comprehensive as drawing in Core Graphics but it can be used in your ViewController implementation.

How to draw a rectangle?

I want to draw a filled rectangle in my viewContoller's view.
I wrote the code below in viewDidLoad. But there is no change.
What is wrong?
CGRect rectangle = CGRectMake(0, 100, 320, 100);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);
You can't do it in a viewController.
You need to extend your View and add the code under "drawRect:"
this will change the drawing logic of your view.
-(void) drawRect:(CGRect)rect{
[super drawRect:rect];
CGRect rectangle = CGRectMake(0, 100, 320, 100);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);
}
modern 2018 solution..
override func draw(_ rect: CGRect) {
let r = CGRect(x: 5, y: 5, width: 10, height: 10)
UIColor.yellow.set()
UIRectFill(r)
}
that's it.
Just for clarity:
If You need to draw a rectangle which has the same fill and border color, then You can replace:
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
with:
[[UIColor redColor] set];
You can't render directly in viewDidLoad; it's the views themselves that would have to run this in their drawRect method.
The easiest way to "draw" a rectangle is to place a UIView with a background color & border in your view. (You can set the border via the view's CALayer's methods. i.e. myView.layer.borderColor = [[UIColor redColor] CGColor];)

Drawing a line with a shadow, but only want to keep shadow. IOS

I am trying to draw a line with a shadow but I do not want to keep the line but only the shadow.
I have attempted to set the stroke color of the line to clear but when I do that the shadow also disappears.
The following code creates 2 lines, i only want to keep the shadow because it looks nicer and its not pixelated like the line.
Is this possible?
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {0.0, 0.0, 0.0, 1.0};
CGColorRef shadowColor = CGColorCreate(colorSpace, components);
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(10,10), 4.0, shadowColor);
Thanks.
So you can't do this directly because the shadow reflects the path, so if you make the path transparent, the shadow will also be transparent. There are a few workarounds I can think of (depends on exactly what you're doing) but one sneaky way would just be to draw the shadow far enough below the path that you can just draw over it, basically erasing it. For example, if the background is white, this will accomplish what you want:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {0.0, 0.0, 0.0, 1.0};
CGColorRef shadowColor = CGColorCreate(colorSpace, components);
CGContextSetShadowWithColor(context, CGSizeMake(0.0f,20.0f), 4.0, shadowColor);
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);
}
hope this helps.
why dont you just draw your line as a shadow. Dont have the shadow drawn but instead draw a black line with .25 or less alpha and offset it to where you would expect the shadow to be.

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);
}