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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:36:26+00:00 2026-06-14T14:36:26+00:00

Usually when you draw in a invalid context, you see something like invalid context

  • 0

Usually when you draw in a invalid context, you see something like invalid context 0x00, but this is not the case, as I see an address. And all my Core Graphics code is inside drawRect of a view.
Sometimes, not always, I see:
“: CGContextFillRects: invalid context 0xa88a500”
Sometimes it seems my code works but others fail.
What I want to draw is something like a mask.

Can you tell me if you see anything wrong here?:

UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextFillRect(context, rect);

switch (shape) {
    case SHAPE_ELIPSE:
        //
        CGContextAddEllipseInRect(context, maskBox);
        CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
        CGContextFillEllipseInRect(context, maskBox);
        break;
    case SHAPE_SQUARE:
        //
        CGContextAddRect(context, maskBox);
        CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
        CGContextFillRect(context, maskBox);
        break;
    default:
        break;
}
CGContextStrokePath(context);
UIImage *backGroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGImageRef maskRef = backGroundImage.CGImage;

CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), NULL, false);

UIGraphicsBeginImageContext(rect.size);
CGContextRef contextBlack = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(contextBlack, [[UIColor blackColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *blackImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGImageRef masked = CGImageCreateWithMask([blackImage CGImage], mask);
CGImageRelease(mask);
[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithCGImage:masked]]];
CGImageRelease(masked);

Thanks a lot.

  • 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-14T14:36:27+00:00Added an answer on June 14, 2026 at 2:36 pm

    Calling UIGraphicsEndImageContext() would invalidate the context context. However, your program continues to use that variable. IOW, you should reassign context after calling UIGraphicsBeginImageContext() the second time:

    context = UIGraphicsGetCurrentContext();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this is usually not the way to use AngularJS but I
Usually in a django view you would do something like queryset = MyModel.objects.something() How
I may not always understand every function in an API but usually I can
Every Christmas we draw names for gift exchanges in my family. This usually involves
When you draw an inheritance diagram you usually go Base ^ | Derived Derived
Usually I worked with PostgreSQL and never had a problem, but now I need
Usually (if not always), when jQuery allows you to add a callback to some
Just want to start out by saying this seems like a great site, hope
I'm overriding -drawRect: to draw some custom views. Usually, I'm drawing a filled rounded
I usually like graphviz a lot for making graphs and trees and outputting them

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.