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.
I have a code, in order to access the particular pixel from an ImageView as follows:
CFDataRef CopyImagePixels(CGImageRef inImage)
{
return CGDataProviderCopyData(CGImageGetDataProvider(inImage));
}
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 :
Getting the pixel value :
NSData *rawData = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(image1.CGImage));
uint *x = malloc (rawData.length);
NSLog(@"Length of raw data :%d", rawData.length);
[rawData getBytes: x];
getting the touches in touchesBegan method :
- (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 ...
}
}
The issue is I cant create a color as above .. Any help would be greatly appreciated ...
Best Regards,
Mohammed Sadiq.