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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:32:04+00:00 2026-05-23T05:32:04+00:00

I’m trying to do something very simple: 1) Draw a UIImage into a CG

  • 0

I’m trying to do something very simple:

1) Draw a UIImage into a CG bitmap context

2) Get a pointer to the data of the image

3) iterate over all pixels and just set all R G B components to 0 and alpha to 255. The result should appear pure black.


This is the original image I am using. 200 x 40 pixels, PNG-24 ARGB premultiplied alpha (All alpha values == 255):

original


This is the result (screenshot from Simulator), when I do not modify the pixels. Looks good:

unmodified result


This is the result, when I do the modifications! It looks like if the modification was incomplete. But the for-loops went over EVERY single pixel. The counter proves it: Console reports modifiedPixels = 8000 which is exactly 200 x 40 pixels. It looks always exactly the same.

modified result

Note: The PNG image I use has no alpha < 255. So no transparent pixels.


This is how I create the context. Nothing special…

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

colorSpace = CGColorSpaceCreateDeviceRGB();

bitmapData = malloc(bitmapByteCount);
bitmapContext = CGBitmapContextCreate(bitmapData,
                width,
                height,
                8, // bits per component
                bitmapBytesPerRow,
                colorSpace,
                CGImageAlphaPremultipliedFirst);

Next, I draw the image into that bitmapContext, and obtain the data like this:

void *data = CGBitmapContextGetData(bitmapContext);

This is the code which iterates over the pixels to modify them:

size_t bytesPerRow = CGImageGetBytesPerRow(img);

NSInteger modifiedPixels = 0;
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        long int offset = bytesPerRow * y + 4 * x;

        // ARGB
        unsigned char alpha = data[offset];
        unsigned char red = data[offset+1];
        unsigned char green = data[offset+2];
        unsigned char blue = data[offset+3];

        data[offset] = 255;
        data[offset+1] = 0;
        data[offset+2] = 0;
        data[offset+3] = 0;

        modifiedPixels++;
    }
}

When done, I obtain a new UIImage from the bitmap context and display it in a UIImageView, to see the result:

CGImageRef imageRef = CGBitmapContextCreateImage(bitmapContext);
UIImage *img = [[UIImage alloc] initWithCGImage:imageRef];

Question:

What am I doing wrong?

Is this happening because I modify the data while iterating over it? Must I duplicate it?

  • 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-23T05:32:05+00:00Added an answer on May 23, 2026 at 5:32 am

    Use CGBitmapContextGetBytesPerRow(bitmapContext) to get bytesPerRow instead getting from image (image has only 3 bytes per pixels if it hasn’t alpha informations)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.