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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:56:43+00:00 2026-05-23T08:56:43+00:00

I want to draw an alpha mask image in code. Right now I do:

  • 0

I want to draw an alpha mask image in code. Right now I do:

1) Create a bitmap context using CGBitmapContextCreate with options CGColorSpaceCreateDeviceRGB and kCGImageAlphaPremultipliedFirst.

2) Then I draw into this context, using only grayscale colors like white and black.

3) Then I create a mask image from that context, using CGImageMaskCreate.

Conclusion: I waste a lot of memory! Because from my understanding, a mask image is grayscale only, right? So why create a context in ARGB in the first place.

How can I create a CGContextRef that is intended to be used for drawing a mask image? My thoughts are to use CGColorSpaceCreateDeviceGray, but here the problems start. This is the exact code how I create my ARGB bitmap context:

CGContextRef    context = NULL;
CGColorSpaceRef colorSpace;
uint32_t *      bitmapData;

int imageWidth = round(size.width);
int imageHeight = round(size.height);

int bitmapBytesPerRow = (imageWidth * 4);
int bitmapByteCount = (bitmapBytesPerRow * imageHeight);

colorSpace = CGColorSpaceCreateDeviceRGB();

bitmapData = malloc(bitmapByteCount);

context = CGBitmapContextCreate(bitmapData,
                                imageWidth,
                                imageHeight,
                                8,  // bits per component
                                bitmapBytesPerRow,
                                colorSpace,
                                kCGImageAlphaPremultipliedFirst);

CGColorSpaceRelease(colorSpace);

I am not sure how to compute the bitmapBytesPerRow for such a context. I assume it would be just imageWidth? And what must I supply for bits per component in CGBitmapContextCreate?

There is CGColorSpaceGetNumberOfComponents() but it reports only the number of components. This does not tell me how many bytes a component has.

Also what makes me nervous is that the 4 and 8 are hard-coded in my code above. Who says it’s always 4 bytes per component, and who says it’s 8 bits per component? I just took this from various sample codes out there. Everyone seems to do it this way. It works. But future proof? Probably not.

You would make my day with some great answers. Thanks.


Edit: I found a code-snippet, but it is confusing:

CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceGray();
CGContextRef gradientBitmapContext = CGBitmapContextCreate (NULL, 1, reflectRect.size.height,8, 0, colorSpace2, kCGImageAlphaNone);

Why 0 for bytes per row? The documentation does not say you can pass 0. Looks wrong.

  • 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-23T08:56:43+00:00Added an answer on May 23, 2026 at 8:56 am

    Those parameters are telling the system how to treat the data or memory you supply. You have created that yourself, so you know what layout you intend. What, if anything, the system might want to do with it behind the scenes is not your immediate problem.

    In this case, you’ll provide 8 bits per sample, with just the 1 component, and probably not want to use any row padding, in which case your bytesPerRow should indeed be the same as the image width.

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

Sidebar

Related Questions

Right now what I can do is draw with touch, using touchesMoved. I can
Want to draw an UIImageView which has an CGImage, and the UIImageView has alpha
I am trying to draw a circle with a glass effect using Alpha. I
I have array with colors as its objects. I want to create an image
Is there a way to manipulate alpha of an image using alpha from another
There is a strange behaviour in my OpenGL code. I want to draw a
I'm using the geom_tile to draw an imagemap and want to clear out the
I want to take a image (a brush) and draw it into a displayed
I want to use an alpha mask in OpenGL so that white(1)=visible and black(0)=hidden.
I want to draw the number badge as shown in the above image on

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.