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

  • Home
  • SEARCH
  • 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 4245786
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:53:11+00:00 2026-05-21T03:53:11+00:00

I am new to Objective C. I am trying to draw(basic lines as of

  • 0

I am new to Objective C.
I am trying to draw(basic lines as of now) onto a bitmap and then have the bitmap rendered on the screen for Iphone.

Following is the code which i tried, it shows me nothing but a blank screen:

- (void)drawRect:(CGRect)rect 
{

CGContextRef screen = UIGraphicsGetCurrentContext();    
size_t width = rect.size.width;

size_t height = rect.size.height;

void *data = malloc(width*height*4); 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef c = CGBitmapContextCreate(data, width, height, 8, width*4, colorSpace, kCGImageAlphaPremultipliedFirst);

CGFloat black[4] = {0.0f,0.0f,0.0f,1.0f}; // setting the color in CMYK format

CGContextSetStrokeColor(c, black);

CGContextBeginPath(c);

CGContextMoveToPoint(c, 5.0f, 5.0f);

CGContextAddLineToPoint(c, 50.0f, 50.0f);

CGContextAddLineToPoint(c, 100.0f, 60.0f);

CGContextMoveToPoint(c, 100.0f, 100.0f);

CGContextAddLineToPoint(c, 50.0f, 150.0f);

CGContextStrokePath(c);

CGImageRef img = CGBitmapContextCreateImage(c);

CFRelease(c);

free(data);

CGContextDrawImage(screen, rect, img);

CGContextFlush(screen);

CGContextFlush(c);

CFRelease(colorSpace);

CGImageRelease(img);

CFRelease(screen);  
}

Let me know if anyone can point out a similar example or suggest changes to my code.


Thanks for the help everybody…
The code works but not as expected.

I modified the code as follows:

CGContextSetRGBFillColor(screen, 1.0, 1.0, 1.0, 1.0); //code works without these 3 lines
CGContextFillRect(screen, rect);
CGContextDrawImage(screen, rect, img);

But there is something weird happening, i can see black lines drawn when the color provided by me is green.
Here is the code after creating the bitmap context:

CGFloat black[4] = {0.0f,1.0f,0.0f,1.0f}; //RGB values
CGContextSetStrokeColor(c, black);

This seems to be the only combination for which the code works…

Does anyone have any clue?

  • 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-05-21T03:53:11+00:00Added an answer on May 21, 2026 at 3:53 am

    First off, you may be aware, but you’re doing an extra step here. Although conceptually this approach will work, the drawRect: gets an implicit graphics context (which you know how to get with UIGraphicsGetCurrentContext) and you can draw directly into that. There’s no need to create the bitmap context and then blit it into the local context afterwards.

    That said, your code generally looks reasonable. You’re drawing in black without clearing the background to anything else, however, which might explain the all-black result. (The underlying bitmap is just the uninitialized memory from malloc unless you do an explicit clear or fill on it.) Try doing a rect fill on the whole context to white, or etc.

    A few other notes:

    1. You’re setting the stroke color in RGBA, not CMYK per your comment, but your result is what you want.
    2. Don’t release the “screen” context– you don’t own that, since you didn’t get it via a “create” method.
    3. I don’t think the flushes are required here, but even if they are, you’re flushing “c” after you release it– make sure you do it before if you want to.
    4. You’re drawing only inside the rect rectangle. This might be fine in your code, but note that this param can be any sub-rect of your view’s bounds. If your intention is to blit the image into the whole view, you should ignore the rect param and just use [self bounds] as the drawing rectangle.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Objective-C and iPhone development, and I'm trying to store floating-point values
I'm very new to Objective-C and I'm trying to develop an iPhone app. My
I'm still pretty new to Objective -C and iPhone development and am trying to
So new to objective-c and iphone/ipad development. Trying to get my feet wet with
I am currently working on an iPhone 2.1 application. I am new to Objective
I am new to objective c and am trying to understand how/when autorelease is
I am new to Objective C and I am trying to implement an async
I am new in objective-c and I am trying to understand memory management to
I'm new to objective-c and Im trying to write a little sample app that
Completely new to Objective-C, trying to find out when I need to alloc and

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.