Mac OS Cocoa: Draw a simple pixel on a canvas

后端 未结 8 648
悲哀的现实
悲哀的现实 2021-02-02 01:21

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

相关标签:
8条回答
  • 2021-02-02 01:38

    Maybe I am misunderstanding the question, but Quartz has the ability to fill rectangles:

    void CGContextFillRect (CGContextRef c, CGRect rect);
    
    0 讨论(0)
  • 2021-02-02 01:44
    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)))
        }
    }
    
    0 讨论(0)
提交回复
热议问题