views:

16

answers:

1

I am trying to draw to a bitmap context but coming up empty. I believe I'm creating things properly because I can initialize the context, draw a few things, then create an image from it and draw that image. What I cannot do is, after initialization, trigger further drawing on the context that draws more items on it. I'm not sure if I'm missing some common practice that implies I can only draw it at certain places or that I have to do something else. Here is what I do, below.

I copied the helper function provided by apple with one modification to obtain the color space because it wasn't compiling (this is for iPad, don't know if that matters):

CGContextRef MyCreateBitmapContext (int pixelsWide, int pixelsHigh)
{
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow   = (pixelsWide * 4);// 1
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);


    colorSpace = CGColorSpaceCreateDeviceRGB();  //CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);// 2

    bitmapData = malloc( bitmapByteCount );// 3
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        return NULL;
    }
    context = CGBitmapContextCreate (bitmapData,// 4
                                    pixelsWide,
                                    pixelsHigh,
                                    8,      // bits per component
                                    bitmapBytesPerRow,
                                    colorSpace,
                                    kCGImageAlphaPremultipliedLast);
    if (context== NULL)
    {
        free (bitmapData);// 5
        fprintf (stderr, "Context not created!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );// 6

    return context;// 7
}

I initialize it in my init method below with a few sample draws just to be sure it looks right:

    mContext = MyCreateBitmapContext (rect.size.width, rect.size.height);

    // sample fills
    CGContextSetRGBFillColor (mContext, 1, 0, 0, 1);
    CGContextFillRect (mContext, CGRectMake (0, 0, 200, 100 ));
    CGContextSetRGBFillColor (mContext, 0, 0, 1, .5);
    CGContextFillRect (mContext, CGRectMake (0, 0, 100, 200 ));


    CGContextSetRGBStrokeColor(mContext, 1.0, 1.0, 1.0, 1.0);
    CGContextSetRGBFillColor(mContext, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(mContext, 5.0);
    CGContextAddEllipseInRect(mContext, CGRectMake(0, 0, 60.0, 60.0));
    CGContextStrokePath(mContext);

In my drawRect method, I create an image from it to render it. Maybe I should be creating and keeping this image as a member var and updating it everytime I draw something new and not creating the image every frame? (Some advice on this would be nice):

// draw bitmap context
CGImageRef myImage = CGBitmapContextCreateImage (mContext);
CGContextDrawImage(context, rect, myImage);
CGImageRelease(myImage);

Then as a test I try drawing a circle when I touch, but nothing happens, and the touch is definitely triggering:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint location;
    for (UITouch* touch in touches)
    {
        location = [touch locationInView: [touch view]];
    }
    CGContextSetRGBStrokeColor(mContext, 1.0, 1.0, 1.0, 1.0);
    CGContextSetRGBFillColor(mContext, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(mContext, 2.0);
    CGContextAddEllipseInRect(mContext, CGRectMake(location.x, location.y, 60.0, 60.0));
    CGContextStrokePath(mContext);
}

Help?

+1  A: 

[self setNeedsDisplay]; !!!!

!!!!!!!!!

so it was because drawRect was never being called after init since it didn't know it needed to refresh. My understanding is that I should just call setNeedsDisplay anytime I draw and that seems to work. :)

Joey