2

I have an imageView and i want to draw lines on image in that imageView using CGPoints but i am getting errors like: invalid context etc.. any solution to draw lines without CGContext approach.

CGContextRef cntxt = UIGraphicsGetCurrentContext(); CGContextMoveToPoint(cntxt, p1.x, p1.y); CGContextAddLineToPoint(cntxt, p2.x, p2.y); CGContextStrokePath(cntxt); 

Thanks

1 Answer 1

1

You are likely attempting to draw outside of drawRect:. If you check the value of cntxt, it is likely NULL. A draw context is created prior to calling drawRect: and is destroyed afterwards. There is generally no "current context" outside of this function.

You need to keep track of your points in a data structure. In drawRect:, you will then do whatever drawing you need to do based on that data.

Sign up to request clarification or add additional context in comments.

3 Comments

yes i am not using drawRect method as i am doing this in UIViewController subclass. drawRect method is available in UIView subclass
UIViewController can't draw anything at all. It's not a view. Your view controller should generally hold onto the relevant data, and pass it to the view for drawing.
Thanks Rob I did it in a UIView class and i got what i wanted.. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.