Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7774117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:17:53+00:00 2026-06-01T17:17:53+00:00

I have followed the sample code on Apple’s Quartz programming guide (Listing 2-5) and

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:17:55+00:00Added an answer on June 1, 2026 at 5:17 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have followed the sample code on the Twilio github page and it doesn't
I have used the NameAndPassword Auth Plugin sample code from Apple to create my
I have followed the sample code below to upload a zip file in the
I followed Apple's GLPaint paint application and tried to modify it.. In sample code
I have followed the instructions found at http://code.google.com/p/pubsubhubbub/wiki/DeveloperGettingStartedGuide to setup a hub. When I
I have followed Apple's documentation on localizing iPhone software, using the genstrings ruby script
Im trying to call python functions from C code, and i followed a sample
Hi I have followed the setup guide at: http://myfaces.apache.org/trinidad/installation.html When I include the following
I've followed instructions http://code.google.com/webtoolkit/doc/latest/tutorial/gettingstarted.html in order to create sample GWT program. Now I would
I was going through code samples of Begining C# 3.0 and followed the sample

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.