I'm trying to make a sprite that uses the graphics that are made with core graphics. I cant seem to find anything to explain how to make shapes using core graphics to create a sprite to use in cocos2d-iPhone.

Thanks for any insights and help!


i don't know cocos2d, so i just can just give you some tricks about CoreGraphics on iOS.

First, the simple case, if you can create sprite from a UIImage or a CGImageRef.

/* function that draw your shape in a CGContextRef */
 void DrawShape(CGContextRef ctx)
     // draw a simple triangle

     CGContextMoveToPoint(ctx, 50, 100);
     CGContextAddLineToPoint(ctx, 100, 0);
     CGContextAddLineToPoint(ctx, 0, 0);

    void CreateImage(void)
       UIGraphicsBeginImageContext(CGSizeMake(100, 100));


       UIImage *image = UIGraphicsGetImageFromCurrentImageContext();


       // now you've got an (autorelease) UIImage, that you can use to
       // create your sprite.
       // use image.CGImage if you need a CGImageRef

If you need to provide a buffer of data, and create your own CGContextRef :

CGContextRef CreateBitmapContextWithData(int width, int height, void *buffer)
    CGContextRef ctx;
    CGColorSpaceRef colorspace;
    size_t bytesPerRow;
    size_t bitsPerComponent;
    size_t numberOfComponents;
    CGImageAlphaInfo alpha;

    bitsPerComponent = 8;
    alpha = kCGImageAlphaPremultipliedLast;

    numberOfComponents = 1;
    colorspace = CGColorSpaceCreateDeviceRGB();

    numberOfComponents += CGColorSpaceGetNumberOfComponents(colorspace);
    bytesPerRow = (width * numberOfComponents);

    ctx = CGBitmapContextCreate(buffer, width, height, bitsPerComponent, bytesPerRow, colorspace, alpha);


    return ctx;

void CreateImageOrGLTexture(void)
   // use pow2 width and height to create your own glTexture ...
   void *buffer = calloc(1, 128 * 128 * 4);
   CGContextRef ctx = CreateBitmapContextWithData(128, 128, buffer);

   // draw you shape

   // you can create a CGImageRef with this context 
   // CGImageRef image = CGBitmapContextCreateImage(ctx);

   // you can create a gl texture with the current buffer
   // glBindTexture(...)
   // glTexParameteri(...)
   // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);


See Apple documentation for all CGContext functions : https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGContext/Reference/reference.html

Hope this help.

  • \$\begingroup\$ Hey Thanks for the detailed reply! however In the cocos2d environment I'm unable to get a valid GRContextRef. \$\endgroup\$ – PeteBob Oct 2 '12 at 16:44
  • \$\begingroup\$ Strange, i use similar function than CreateBitmapContextWithData a lot on iOS with or without OpenGL ES, in command line tools on Mac OS ... Do you have errors log on console ? \$\endgroup\$ – Emmanuel Oct 2 '12 at 16:54
  • \$\begingroup\$ Spoke to soon! It actually works great! Thanks for the help :) I did have to create my own bitmap context though. \$\endgroup\$ – PeteBob Oct 2 '12 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.