Mac OS Cocoa: Draw a simple pixel on a canvas

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

    What you are asking for is either of these two methods:

    NSBitmapRep setColor:atX:y: Changes the color of the pixel at the specified coordinates.

    NSBitmapRep setPixel:atX:y: Sets the receiver's pixel at the specified coordinates to the specified raw pixel values.

    Note that these aren't available on iOS. On iOS, it appears that the way to do this is to create a raw buffer of pixel data for a given colorspace (likely RGB), fill that with color data (write a little setPixel method to do this) and then call CGImageCreate() like so:

        //Create a raw buffer to hold pixel data which we will fill algorithmically
        NSInteger width = theWidthYouWant;
        NSInteger height = theHeightYouWant;
        NSInteger dataLength = width * height * 4;
        UInt8 *data = (UInt8*)malloc(dataLength * sizeof(UInt8));
    
        //Fill pixel buffer with color data
        for (int j=0; j

    Lastly, you might be wanting to manipulate pixels in an image you've already loaded into a CGImage. There is sample code for doing that in an Apple Technical Q&A titled QA1509 Getting the pixel data from a CGImage object.

提交回复
热议问题