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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:03:19+00:00 2026-05-27T15:03:19+00:00

I am using GraphicsContext for 2d drawing. For some operations i need to edit

  • 0

I am using GraphicsContext for 2d drawing. For some operations i need to edit individual pixels. However, something strange is happening, my pixels are not preserved as they should. I draw red rectangle, half transparent – red is set to 1(255) and transparency to 0.5(128). When i read pixels later on – red is now 128(half its value) and alpha is 128(which is correct). I want to later on find all red pixels with value 255, and change them, but that is not possible, since they are not retaining the set value. Here is my code:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(widthT, heightT), NO, 1);
CGContextRef contextRef = UIGraphicsGetCurrentContext();
//CGContextSaveGState(contextRef);
CGContextSetRGBFillColor(contextRef, 1, 0, 0, 0.5);
CGContextSetRGBStrokeColor(contextRef, 1, 0, 0, 0.5);
CGContextFillRect(contextRef, CGRectMake(0, 0, 400, 400));

UIImage *imageMoonMask = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//NSData *data = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));

NSData *data = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(imageMoonMask.CGImage));

Byte *copyPixels = (Byte *)[data bytes];
pixels = (Byte *) malloc([data length] * sizeof (Byte));

for(int i = 0; i < [data length]; i += 4) {
    pixels[i] = copyPixels[i];
    pixels[i+1] = copyPixels[i+1];
    pixels[i+2] = copyPixels[i+2]; //red color
    pixels[i+3] = copyPixels[i+3]; //alpha
    NSLog(@"Originalni");
    NSLog(@"%u", pixels[i]);
    NSLog(@"%u", pixels[i+1]);
    NSLog(@"%u", pixels[i+2]);
    NSLog(@"%u", pixels[i+3]);
}
  • 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-27T15:03:20+00:00Added an answer on May 27, 2026 at 3:03 pm

    The bytes are stored in premultiplied alpha format, to allow more efficient alpha blending. You could avoid premultiplied alpha by creating a custom bitmap context via Core Graphics e.g.
    CGBitmapContextCreate and use kCGImageAlphaLast instead of kCGImageAlphaPremultipliedLast…

    But it may be easier to recover the original value just by using

    CGFloat red = copyPixels[i+2] * 255.0f / copyPixels[i];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little method that does some CoreGraphics drawing. Today I was using
Using C# .NET 3.5 and WCF, I'm trying to write out some of the
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C#, I need a class called User that has a username, password, active
i want to show text using quartz 2d. Text will be drawing randomly on
We are using graphics context of a form to draw an image. However it
I need to draw on my uiview without using the drawrect method but i
I am developing iPhone game application using one UIView and GraphicsContext. The belows are
using a binary search tree I need to add to a vector all int
Using Browserlab, it appears that the background image is not centred in Firefox7 for

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.