creating a color from the pixel value of the image
<p>I am trying to implement an application in which I display a color image as backgroundColor of the custom View.Once the user touches a particular area on the image (dispalyed as background color of the view),  the pixels where the touch has occured should be retrieved and the corresponding color should be created using UIColor.</p>

<p>I have a code, in order to access the particular pixel from an ImageView as follows:</p>

<pre><code>CFDataRef CopyImagePixels(CGImageRef inImage)
{
    return CGDataProviderCopyData(CGImageGetDataProvider(inImage));
}
</code></pre>

<p>From the above code , I got the raw pixel data of the image. Then I created a custom UIView , and the particular Images is set as the background color of the custom view. Then I captured the Touches using the method  "handleTouchesBegan" . Then i got the x and y co-ordinates from the parameters. The code is as follows :</p>

<p>Getting the pixel value :</p>

<pre><code> NSData *rawData = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(image1.CGImage));
 uint *x = malloc (rawData.length);
 NSLog(@"Length of raw data :%d", rawData.length);
 [rawData getBytes: x];
</code></pre>

<p>getting the touches in touchesBegan method :</p>

<pre><code>- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    for (UITouch *touch in touches) 
    {
     // Sends to the dispatch method, which will make sure the appropriate subview is acted upon
     CGPoint pt =  [touch locationInView:upperImage];
     indexValue = pt.x * pt.y;

     int alpha = x[indexValue];
                  int red = x[indexValue + 1];
                  int green = x[indexValue + 2];
                  int blue = x[indexValue + 3];

     self.view.backgroundColor =  [ UIColor  colorWithRed:red green:green blue:blue alpha:alpha];
     // TODO : compare the values with raw image Bytes ...
    }
}
</code></pre>

<p>The issue is I cant create a color as above .. Any help would be greatly appreciated ...</p>

<p>Best Regards,</p>

<p>Mohammed Sadiq.</p>
