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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:47:25+00:00 2026-05-11T11:47:25+00:00

I am writing an iPhone application and need to essentially implement something equivalent to

  • 0

I am writing an iPhone application and need to essentially implement something equivalent to the ‘eyedropper’ tool in photoshop, where you can touch a point on the image and capture the RGB values for the pixel in question to determine and match its color. Getting the UIImage is the easy part, but is there a way to convert the UIImage data into a bitmap representation in which I could extract this information for a given pixel? A working code sample would be most appreciated, and note that I am not concerned with the alpha value.

  • 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. 2026-05-11T11:47:26+00:00Added an answer on May 11, 2026 at 11:47 am

    A little more detail…

    I posted earlier this evening with a consolidation and small addition to what had been said on this page – that can be found at the bottom of this post. I am editing the post at this point, however, to post what I propose is (at least for my requirements, which include modifying pixel data) a better method, as it provides writable data (whereas, as I understand it, the method provided by previous posts and at the bottom of this post provides a read-only reference to data).

    Method 1: Writable Pixel Information

    1. I defined constants

      #define RGBA        4 #define RGBA_8_BIT  8 
    2. In my UIImage subclass I declared instance variables:

      size_t bytesPerRow; size_t byteCount; size_t pixelCount;  CGContextRef context; CGColorSpaceRef colorSpace;  UInt8 *pixelByteData; // A pointer to an array of RGBA bytes in memory RPVW_RGBAPixel *pixelData; 
    3. The pixel struct (with alpha in this version)

      typedef struct RGBAPixel {     byte red;     byte green;     byte blue;     byte alpha; } RGBAPixel; 
    4. Bitmap function (returns pre-calculated RGBA; divide RGB by A to get unmodified RGB):

      -(RGBAPixel*) bitmap {     NSLog( @'Returning bitmap representation of UIImage.' );     // 8 bits each of red, green, blue, and alpha.     [self setBytesPerRow:self.size.width * RGBA];     [self setByteCount:bytesPerRow * self.size.height];     [self setPixelCount:self.size.width * self.size.height];      // Create RGB color space     [self setColorSpace:CGColorSpaceCreateDeviceRGB()];      if (!colorSpace)     {         NSLog(@'Error allocating color space.');         return nil;     }      [self setPixelData:malloc(byteCount)];      if (!pixelData)     {         NSLog(@'Error allocating bitmap memory. Releasing color space.');         CGColorSpaceRelease(colorSpace);          return nil;     }      // Create the bitmap context.      // Pre-multiplied RGBA, 8-bits per component.      // The source image format will be converted to the format specified here by CGBitmapContextCreate.     [self setContext:CGBitmapContextCreate(                                            (void*)pixelData,                                            self.size.width,                                            self.size.height,                                            RGBA_8_BIT,                                            bytesPerRow,                                            colorSpace,                                            kCGImageAlphaPremultipliedLast                                            )];      // Make sure we have our context     if (!context)   {         free(pixelData);         NSLog(@'Context not created!');     }      // Draw the image to the bitmap context.      // The memory allocated for the context for rendering will then contain the raw image pixelData in the specified color space.     CGRect rect = { { 0 , 0 }, { self.size.width, self.size.height } };      CGContextDrawImage( context, rect, self.CGImage );      // Now we can get a pointer to the image pixelData associated with the bitmap context.     pixelData = (RGBAPixel*) CGBitmapContextGetData(context);      return pixelData; } 

    Read-Only Data (Previous information) – method 2:


    Step 1. I declared a type for byte:

     typedef unsigned char byte; 

    Step 2. I declared a struct to correspond to a pixel:

     typedef struct RGBPixel{     byte red;     byte green;     byte blue;       }    RGBPixel; 

    Step 3. I subclassed UIImageView and declared (with corresponding synthesized properties):

    //  Reference to Quartz CGImage for receiver (self)   CFDataRef bitmapData;     //  Buffer holding raw pixel data copied from Quartz CGImage held in receiver (self)     UInt8* pixelByteData;  //  A pointer to the first pixel element in an array     RGBPixel* pixelData; 

    Step 4. Subclass code I put in a method named bitmap (to return the bitmap pixel data):

    //Get the bitmap data from the receiver's CGImage (see UIImage docs)   [self setBitmapData: CGDataProviderCopyData(CGImageGetDataProvider([self CGImage]))];  //Create a buffer to store bitmap data (unitialized memory as long as the data)     [self setPixelBitData:malloc(CFDataGetLength(bitmapData))];  //Copy image data into allocated buffer     CFDataGetBytes(bitmapData,CFRangeMake(0,CFDataGetLength(bitmapData)),pixelByteData);  //Cast a pointer to the first element of pixelByteData     //Essentially what we're doing is making a second pointer that divides the byteData's units differently - instead of dividing each unit as 1 byte we will divide each unit as 3 bytes (1 pixel).     pixelData = (RGBPixel*) pixelByteData;  //Now you can access pixels by index: pixelData[ index ]     NSLog(@'Pixel data one red (%i), green (%i), blue (%i).', pixelData[0].red, pixelData[0].green, pixelData[0].blue);  //You can determine the desired index by multiplying row * column.     return pixelData; 

    Step 5. I made an accessor method:

    -(RGBPixel*)pixelDataForRow:(int)row column:(int)column{     //Return a pointer to the pixel data     return &pixelData[row * column];            } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer nerddinner has example code to download. May 11, 2026 at 8:26 pm
  • Editorial Team
    Editorial Team added an answer This might not be appropriate, since your problem is different,… May 11, 2026 at 8:26 pm
  • Editorial Team
    Editorial Team added an answer Ensure that all your CS files are within the same… May 11, 2026 at 8:26 pm

Related Questions

I am writing an iPhone application, and need to save the state of my
I was hoping to implement a simple XMPP server in Java. What I need
I am writing an iPhone application which requires the user to enter their mobile
Is it possible to have a C static library API, which uses C++ internally

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.