1

Possible Duplicate:
How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?
Get Pixel color of UIImage

I have a scenario in which the user can select a color from an image, for example the one below:enter image description here

Depending on the tap location of the user on the image I need to extract the RGB and alpha value at that very point—or say, pixel. How do I accomplish this?

PDK
  • 1,476
  • 1
  • 14
  • 25
Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50

1 Answers1

1

You need to create a bitmap context (CGContextRef) from the image and convert the CGPoint that was tapped to an array offset location to retrieve the color information from the pixel data.

See What Color is My Pixel? for a tutorial and this similar Stack Overflow question.

Methods:

- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage

Returns a CGContextRef representing the image passed as an argument using the correct color space. This method is called by:

- (UIColor*) getPixelColorAtLocation:(CGPoint) point

This is the function you would call to get the UIColor at the passed CGPoint.

Note that these methods are in a subclass of a UIImageView to make the process more straightforward.

Community
  • 1
  • 1
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144