Mac OS Cocoa: Draw a simple pixel on a canvas

后端 未结 8 659
悲哀的现实
悲哀的现实 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:24

    Here's a quick way to draw pixels on OS X:

    - (void)drawRect:(NSRect)dirtyRect {
        [super drawRect:dirtyRect];
    
        NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
                                 initWithFocusedViewRect:dirtyRect];
    
        for (int x = 0; x < [rep pixelsWide]; x++) {
            for (int y = 0; y < [rep pixelsHigh]; y++) {
                NSUInteger pixel[4] = { 0, 255, 0, 255 };
                [rep setPixel:pixel atX:x y:y];
            }
        }
    
        [rep drawInRect:dirtyRect];
    }
    

提交回复
热议问题