0

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.

mahboudz
  • 39,196
  • 16
  • 97
  • 124
  • If you're doing pixel selection, you might find this link interesting. It creates a magnifier loupe view of the pixels around the touch area. http://www.craftymind.com/2009/02/10/creating-the-loupe-or-magnifying-glass-effect-on-the-iphone/ . Also, please mark more of your questions answered. At least a few of them have appropriate answers but you're not closing them. – wkw Nov 14 '09 at 14:14

2 Answers2

0

The alpha value may be at the beginning or the end depending on the format of the bitmap. The RGB values may also be premultiplied with the alpha value. You can use CGImageGetBitmapInfo to get more info on the format of the bitmap.

Print out the RGB values and see if they look right.

mahboudz
  • 39,196
  • 16
  • 97
  • 124
0

Sounds like an issue similar to this question.

Community
  • 1
  • 1
Ramin
  • 13,343
  • 3
  • 33
  • 35