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 666505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:49:33+00:00 2026-05-13T23:49:33+00:00

I asked this question earlier and am now trying to explore the idea of

  • 0

I asked this question earlier and am now trying to explore the idea of making an offscreen image.

Something is going wrong – I think maybe with colour space? I’ve boiled the code down and down and down, until I eventually can demonstrate the problem with just a few lines.

I have a view with an imageview (called iv) and a button, which when pushed, calls “push”


- (UIImage *) block:(CGSize)size {
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColor(context, CGColorGetComponents([UIColor redColor].CGColor));
    CGContextFillRect (context, CGRectMake(0, 0, size.width, size.height));
    UIImage *result =  UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();
    return result;  
}


- (IBAction) push {
    self.iv.image = [self block:CGSizeMake(50.0,50.0)];
}

The issue here is that instead of drawing a 50×50 block in the specified colour, it draws it in a shade of grey, which leads me to think its a colour space error.

I tried the same thing with a Bitmap Context using


- (CGContextRef) createBitmapContextOfSize:(CGSize) size {
    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    bitmapBytesPerRow   = (size.width * 4);
    bitmapByteCount     = (bitmapBytesPerRow * size.height);

    colorSpace = CGColorSpaceCreateDeviceRGB();
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL) {
        fprintf (stderr, "Memory not allocated!");
        return NULL;
    }
    context = CGBitmapContextCreate (bitmapData,
                                     size.width,
                                     size.height,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedLast);
    //CGContextSetAllowsAntialiasing (context,NO);
    if (context== NULL) {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
        return NULL;
    }
    CGColorSpaceRelease( colorSpace );
    return context;
}

- (UIImage *) block:(CGSize)size {  
    UIGraphicsBeginImageContext(size);
    CGContextRef context = [self createBitmapContextOfSize:size];
    CGContextSetFillColor(context, CGColorGetComponents([UIColor blueColor].CGColor));
    CGContextFillRect (context, CGRectMake(0, 0, size.width, size.height));
    UIImage *result = [UIImage imageWithCGImage: CGBitmapContextCreateImage (context)];
    CGContextRelease(context);
    UIGraphicsEndImageContext();
    return result;
}

with identical results. Grey boxes not (in this case) a blue box.

I’m sure if I can get this to work everything else will follow.

  • 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-13T23:49:33+00:00Added an answer on May 13, 2026 at 11:49 pm

    If you have a CGColor object, just use the CGContextSetFillColorWithColor function.

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

Sidebar

Related Questions

You may think this question is like this question asked on StackOverflow earlier. But
Earlier I asked this question How to correctly unit test my DAL? , one
This is in reference to a question I asked earlier. Aside from viewing the
This is an attempt to rephrase a question I asked earlier. I'd like to
I asked this question before, Here however I think I presented the problem poorly,
I asked this question a while back but now I'm looking to implement an
I asked this question a bit earlier and have only been able to gain
Earlier today I asked this question . After spending some time investigating this issue,
So earlier I asked this question: JQuery Login Redirect. Code Included The php file
I know I have asked this question in a different manner earlier today but

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.