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.
I am writing an iPhone application and need to essentially implement something equivalent to
Share
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
I defined constants
In my UIImage subclass I declared instance variables:
The pixel struct (with alpha in this version)
Bitmap function (returns pre-calculated RGBA; divide RGB by A to get unmodified RGB):
Read-Only Data (Previous information) – method 2:
Step 1. I declared a type for byte:
Step 2. I declared a struct to correspond to a pixel:
Step 3. I subclassed UIImageView and declared (with corresponding synthesized properties):
Step 4. Subclass code I put in a method named bitmap (to return the bitmap pixel data):
Step 5. I made an accessor method: