vacioarconte
vacioarconte

Reputation: 1

How to implement CGContext into actual code?

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint endlocation = [touch locationInView:touch.view];
UIImage* thing;
UIImageView* thing2 = [[UIImageView alloc] init];
UIImageView* thing3=[[UIImageView alloc] init];
switch ([_diagramselect selectedSegmentIndex]) {
    case 0:
        thing = [UIImage imageNamed:@"makeO"];
        [thing2 setFrame:CGRectMake(endlocation.x-72,endlocation.y-172,25,25)];
        [thing2 setImage:thing];
        [_field addSubview:thing2];
        break;
    case 1:
        thing = [UIImage imageNamed:@"missX"];
        [thing2 setFrame:CGRectMake(endlocation.x-72,endlocation.y-172,25,25)];
        [thing2 setImage:thing];
        [_field addSubview:thing2];
        break;
    case 2:
        CGContextRef *context = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColor(context,black);
        CGContextBeginPath(context);
        CGContextMoveToPoint(context,startlocation.x,startlocation.y);
        CGContextAddLineToPoint(context,endlocation.x,endlocation.y);
        CGContextStrokePath(context);





        /*thing = [UIImage imageNamed:@"passArrow"];
        [thing3 setFrame:CGRectMake(startlocation.x-72,startlocation.y-172,endlocation.x-startlocation.x,endlocation.y-startlocation.y)];
        [thing3 setImage:thing];
        [_field addSubview:thing3];
      */

        break;
    case 3:
        thing = [UIImage imageNamed:@"trussArrow"];
        break;

    default:
        break;
}

Case 2 is intended to draw from the initial location of the tap to the end location of the tap. However the following code gives an error of "expected expression" on the line with CGContextRef and as a result the following lines do not recognize context. What is the issue here? Is there a specific view that this code needs to target, and if so how would I do this? Thanks!

Upvotes: 0

Views: 65

Answers (1)

brandonscript
brandonscript

Reputation: 72875

Try without the pointer reference asterisk:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColor(context,black);
CGContextBeginPath(context);
CGContextMoveToPoint(context,startlocation.x,startlocation.y);
CGContextAddLineToPoint(context,endlocation.x,endlocation.y);
CGContextStrokePath(context);

Furthermore, you may want to look into UIGraphicsBeginImageContext() and UIGraphicsEndImageContext() depending on what you're trying to do.

Upvotes: 1

Related Questions