Looking for a guide on Cocoa graphics

Discussion in 'Mac Programming' started by monsieurpaul, Mar 27, 2011.

  1. monsieurpaul macrumors regular

    Joined:
    Oct 8, 2009
    #1
    Hello,

    I am starting to be really confused with Image, ImageRep, View, GraphicsContext, etc.

    On one hand, what I have found in books and blogs focuses mainly on recipes. On the other hand, I am finding the Cocoa Drawing Guide from Apple documentation really overwhelming.

    Does anyone has something else to recommend ?

    Thanks,
    Paul
     
  2. jiminaus, Mar 27, 2011
    Last edited: Mar 27, 2011
  3. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #3
    I was able to get an image displayed based on a bitmap my program computed. The code looked something like this (paraphrasing)...

    Code:
            NSBitmapImageRep *myBitmap;
            NSImage *myImage;
            unsigned char *buff[4];
            unsigned char *pixels;
    
            pixels = malloc(rectSize * 4);
    
            for(y = 0; y < height; y++)
            {
                    for(x = 0; x < width; x++)
                    {
                            // do stuff
                            pixels[i++] = red;
                            pixels[i++] = green;
                            pixels[i++] = blue;
                            pixels[i++] = 255;   // Alpha channel
                    }
            }
    
            buff[0] = pixels;
    
            myBitmap = [[NSBitmapImageRep alloc]
                      initWithBitmapDataPlanes:buff 
                      pixelsWide:width
                      pixelsHigh:height
                      bitsPerSample:8
                      samplesPerPixel:4
                      hasAlpha:YES
                      isPlanar:NO
                      colorSpaceName:NSCalibratedRGBColorSpace
                      bitmapFormat:0
                      bytesPerRow:(4 * width)
                      bitsPerPixel:32];
    
            myImage = [[NSImage alloc] init];
            [myImage addRepresentation:myBitmap];
            [display setImage: myImage];
            [myImage release];
            [myBitmap release];
    }
    
    Where "display" is an outlet to a View which contains an image.
     
  4. mif macrumors regular

    Joined:
    Feb 16, 2010
    Location:
    home
  5. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
  6. mif macrumors regular

    Joined:
    Feb 16, 2010
    Location:
    home
    #6
    Thanks. it may get me started learning Cocoa.
     

Share This Page