I wish I would find an answer for this. I have searched and searched and couldn\'t the right answer. Here is my situation:
In a Mac OS Cocoa Application, I want to draw
Maybe I am misunderstanding the question, but Quartz has the ability to fill rectangles:
void CGContextFillRect (CGContextRef c, CGRect rect);
class CMKViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let testIV = TestImageView(image: NSImage(named: "test.png")!)
// testIV.frame = CGRect(x:0,y:0,width:100,height:100)
self.view.addSubview(testIV)
testIV.myPixels = [CGPoint(x:10,y:10)]
}
}
class TestImageView:NSImageView{
var myPixels:[CGPoint] = []
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
guard let context: CGContext = NSGraphicsContext.current()?.cgContext else {
consolePrint("Cannot get graphics context")
return
}
// Fill background to white
context.setFillColor(.white)
context.fill(bounds)
context.setFillColor(NSColor.red.cgColor)
// Draw pixels
context.fillPixels(myPixels)
}
}
extension CGContext {
func fillPixels(_ pixels: [CGPoint]) {
var size:CGSize?
if Screen.retinaScale > 1{
size = CGSize(width: 1.5, height: 1.5)
}else{
size = CGSize(width: 1.0, height: 1.0)
}
for pixel in pixels{
fill(CGRect(origin: pixel, size: size!))
}
}
func fill(_ pixel: CGPoint) {
fill(CGRect(origin: pixel, size: CGSize(width: 1.0, height: 1.0)))
}
}