Search code examples
objective-ccore-graphicsquartz-graphicsquartz-2d

How to draw using a bitmap context


I have followed the sample code on Apple's Quartz programming guide (Listing 2-5) and (after correcting a couple typos in the code like a calloc with just one argument that should have been malloc) I have this function defined above my @implementation:

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();
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
 }

Then I have a method as follows:

-(void)testDrawCG{

CGRect myBoundingBox;// 1

CGContextRef myBitmapContext;
CGImageRef myImage;



myBoundingBox = CGRectMake (100, 100, 100, 100);// 2
myBitmapContext = MyCreateBitmapContext (400, 300);// 3
// ********** Your drawing code here ********** // 4
CGContextSetRGBFillColor (myBitmapContext, 1, 0, 0, 1);
CGContextFillRect (myBitmapContext, CGRectMake (0, 0, 200, 100 ));
CGContextSetRGBFillColor (myBitmapContext, 0, 0, 1, .5);
CGContextFillRect (myBitmapContext, CGRectMake (0, 0, 100, 200 ));
myImage = CGBitmapContextCreateImage (myBitmapContext);// 5
CGContextDrawImage(myBitmapContext, myBoundingBox, myImage);// 6
char *bitmapData = CGBitmapContextGetData(myBitmapContext); // 7
CGContextRelease (myBitmapContext);// 8
if (bitmapData) free(bitmapData); // 9
CGImageRelease(myImage);


 }

Everything compiles fine, but nothing appears when I call the method.

My understanding is that unlike drawInRect which requires UIView to be in the picture (no pun intended), this bitmap drawing can take place from any type of class, even if you are not using UIKit.

In this example, I simply calling the method from viewDidLoad as a test, but I'd think I can call it from anywhere, even an NSObject subclass, and expect to see something; at least, that is what is suggested by Apple's docs. Any thoughts?

UPDATE: Further reading in the Apple docs led me to try this to create the context, rather than use the custom function previously defined:

 //    myBitmapContext = MyCreateBitmapContext (400, 300);// 3
UIGraphicsBeginImageContext(myBoundingBox.size);
myBitmapContext=UIGraphicsGetCurrentContext();

However, the results are still nothing appearing on the screen. Additionally, I get this in the log even though I am not directly calling malloc:

 malloc: *** error for object 0x102b1020: pointer being freed was not allocated*** set a breakpoint in malloc_error_break to debug

Solution

  • You're confused about what it takes to draw something on-screen. You need a view of some kind -- either a UIView in an iOS app, or an NSView in a Cocoa app.

    Both kinds of views generally require that you draw into them by implementing the -drawRect: method. You cannot (easily) draw into them at other times. (This is a vast overgeneralization, really, but it's close enough.)

    Alternatively, save your image to a file, then open the file. (For instance, wrap the CGImageRef in a UIImage, then use UIImagePNGRepresentation to create PNG data, then -[NSData writeToFile:atomically:] to write it to a file.)

    Your code also has a few issues. -testDrawCG is OK until here:

        myImage = CGBitmapContextCreateImage (myBitmapContext);// 5
    

    You've taken the contents of the bitmap context -- the two rectangles you drew earlier -- and created a CGImageRef from them. Think of this image as a snapshot of the context.

        CGContextDrawImage(myBitmapContext, myBoundingBox, myImage);// 6
    

    Now you're drawing that image back into the same context. I have no idea why you would do this. Just skip it.

        char *bitmapData = CGBitmapContextGetData(myBitmapContext); // 7
    

    Now you're getting the raw data that backs the bitmap context. You don't need to do this.

        CGContextRelease (myBitmapContext);// 8
    

    OK, you're done with the context, makes sense.

        if (bitmapData) free(bitmapData); // 9
    

    Don't do this! You did not get the data using a method with Create or Copy in the name, therefore you don't own it and you shouldn't free it.

        CGImageRelease(myImage);
    

    OK, fine, you're done with the image so you release it. But since you did nothing useful with the image then it's no surprise that you don't see it appearing anywhere.