I am trying to draw on a view with the mouse events but xcode is giving me "invalid context". Could anyone tell when I'm doing wrong?
class DrawingView: NSView {
var gContext:CGContextRef?
var lastPoint = NSPoint(x:0, y:0)
var red: CGFloat = 0.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var brushWidth: CGFloat = 10.0
var opacity: CGFloat = 1.0
var swiped = false
func drawLineFrom(p1:NSPoint, p2:NSPoint){
// CGContextBeginPath(gContext)
// CGContextSetRGBStrokeColor(gContext, 0, 0, 0, 1)
NSColor.redColor().set() // choose color
CGContextSetLineWidth(gContext, 10.0)
CGContextMoveToPoint(gContext, p1.x, p1.y)
CGContextAddLineToPoint(gContext, p2.x, p2.y)
CGContextStrokePath(gContext)
// self.setNeedsDisplayInRect(self.bounds)
needsDisplay = true
}
override func mouseUp(theEvent: NSEvent) {
swiped = true
let droppings = theEvent.locationInWindow
let currentPoint:NSPoint = self.convertPoint(droppings, fromView: nil)
self.drawLineFrom(lastPoint, p2: currentPoint)
Swift.print("Up \(currentPoint)")
lastPoint = currentPoint
}
override func mouseDown(theEvent: NSEvent) {
// let position = theEvent.locationInWindow
// Swift.print("Mouse Down: \(position.x) - \(position.y)")
if theEvent.clickCount == 1{
let point:NSPoint = self.convertPoint(theEvent.locationInWindow, fromView: self)
Swift.print("Point >> \(point)")
lastPoint = point
}
}
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
// Drawing code here.
let context = NSGraphicsContext.currentContext()?.CGContext
let rawContext = NSGraphicsContext.currentContext()
rawContext?.saveGraphicsState()
self.gContext = context
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1)
CGContextSetLineWidth(context, 10.0)
CGContextMoveToPoint(context, 0, 0)
CGContextAddLineToPoint(context, 200, 200)
CGContextStrokePath(context)
}
And here is my debug output...
Point >> (247.71484375, 108.34375) Oct 25 15:40:07 OSX Basics[17113] : CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. Oct 25 15:40:07 OSX Basics[17113] : CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. Up (394.96484375, 220.5078125)
You cannot draw the content of a view outside the draw rect method. The error is due to the code below :
func drawLineFrom(p1:NSPoint, p2:NSPoint) {
NSColor.redColor().set() // choose color
// ....
}
You call drawLineFrom(:) from the mouse up event. You attempt to change the color of the current graphics context when no context exists. To redraw a view, you must ask a redraw by doing self.needsDisplay = true and then make the changes inside the draw rect method.
Here is a sample code :
class DrawingView: NSView {
var point: CGPoint = CGPoint.zero
override func mouseUp(theEvent: NSEvent) {
// Convert the point from the window coordinate to the view coordinate
self.point = self.convertPoint(theEvent.locationInWindow, fromView: nil)
// We ask to redraw the view
self.needsDisplay = true
Swift.print("Up \(self.point)")
}
}
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
// You have to draw inside this function and not outside
// The context is only valid here and you cannot retain it to draw later
let context = NSGraphicsContext.currentContext()?.CGContext
// Draw here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With