Hi,
I'm trying to get the pixel color of a color wheel at a touch point. I set the color wheel image in drawRect, along with the bitmap context and data. I need to set the color wheel image (vs using an image on file) because I use an overlay to make the color wheel darker to get black. Then in the touches____ methods, I find the pixel in the bitmap data. The code is here:
Code:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGImageRef wheel = [UIImage imageNamed:@"ColorWheel"].CGImage;
CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(rect.size.width / 2 - 140.0, rect.size.height / 2 - 140.0, 280.0, 280.0), wheel);
UIGraphicsBeginImageContext(rect.size);
currentColorWheel = UIGraphicsGetImageFromCurrentImageContext().CGImage;
UIGraphicsEndImageContext();
//Thanks to http://www.markj.net/iphone-uiimage-pixel-color/
CGColorSpaceRef colorSpace;
int bitmapByteCount, bitmapBytesPerRow;
size_t width = CGImageGetWidth(currentColorWheel),
height = CGImageGetHeight(currentColorWheel);
bitmapBytesPerRow = width * 4;
bitmapByteCount = bitmapBytesPerRow * height;
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
NSLog(@"Color space could not be created.");
return;
}
currentBitmapContext = CGBitmapContextCreate(NULL, width, height, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst);
if (currentBitmapContext == NULL)
NSLog(@"Bitmap context could not be created.");
CGContextDrawImage(currentBitmapContext, CGRectMake(0.0, 0.0, width, height), currentColorWheel);
imageData = CGBitmapContextGetData(currentBitmapContext);
if (imageData == NULL)
{
NSLog(@"Image data could not be created.");
}
CGColorSpaceRelease(colorSpace);
}
//...
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self];
if (DistanceBetweenPoints(CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2), touchPoint) <= 138.0) //Check if the point is within the radius
{
if (currentBitmapContext != NULL && imageData != NULL)
{
size_t width = CGImageGetWidth(currentColorWheel);
int index = 4 * (width * round(touchPoint.y) + round(touchPoint.x));
NSLog(@"Index: %i size: %lu", index, sizeof(imageData) / sizeof(unsigned char));
UIColor *color = [UIColor colorWithRed:imageData[index] green:imageData[index + 1] blue:imageData[index + 2] alpha:imageData[index + 3]];
NSLog(@"Color: %@", color);
}
}
}
Strangely, the log about the index and size in touchesEnded shows that imageData has a size of only 4 unsigned chars! As a result, the color is 0, 0, 0, 0. Any ideas on why this is happening and how I can get a correct color?
Hi,
I'm trying to get the pixel color of a color wheel at a touch point. I set the color wheel image in drawRect, along with the bitmap context and data. I need to set the color wheel image (vs using an image on file) because I use an overlay to make the color wheel darker to get black. Then in the touches____ methods, I find the pixel in the bitmap data. The code is here:
Code:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGImageRef wheel = [UIImage imageNamed:@"ColorWheel"].CGImage;
CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(rect.size.width / 2 - 140.0, rect.size.height / 2 - 140.0, 280.0, 280.0), wheel);
UIGraphicsBeginImageContext(rect.size);
currentColorWheel = UIGraphicsGetImageFromCurrentImageContext().CGImage;
UIGraphicsEndImageContext();
//Thanks to http://www.markj.net/iphone-uiimage-pixel-color/
CGColorSpaceRef colorSpace;
int bitmapByteCount, bitmapBytesPerRow;
size_t width = CGImageGetWidth(currentColorWheel),
height = CGImageGetHeight(currentColorWheel);
bitmapBytesPerRow = width * 4;
bitmapByteCount = bitmapBytesPerRow * height;
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
NSLog(@"Color space could not be created.");
return;
}
currentBitmapContext = CGBitmapContextCreate(NULL, width, height, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst);
if (currentBitmapContext == NULL)
NSLog(@"Bitmap context could not be created.");
CGContextDrawImage(currentBitmapContext, CGRectMake(0.0, 0.0, width, height), currentColorWheel);
imageData = CGBitmapContextGetData(currentBitmapContext);
if (imageData == NULL)
{
NSLog(@"Image data could not be created.");
}
CGColorSpaceRelease(colorSpace);
}
//...
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self];
if (DistanceBetweenPoints(CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2), touchPoint) <= 138.0) //Check if the point is within the radius
{
if (currentBitmapContext != NULL && imageData != NULL)
{
size_t width = CGImageGetWidth(currentColorWheel);
int index = 4 * (width * round(touchPoint.y) + round(touchPoint.x));
NSLog(@"Index: %i size: %lu", index, sizeof(imageData) / sizeof(unsigned char));
UIColor *color = [UIColor colorWithRed:imageData[index] green:imageData[index + 1] blue:imageData[index + 2] alpha:imageData[index + 3]];
NSLog(@"Color: %@", color);
}
}
}
Strangely, the log about the index and size in touchesEnded shows that imageData has a size of only 4 unsigned chars! As a result, the color is 0, 0, 0, 0. Any ideas on why this is happening and how I can get a correct color?
Thanks!
If you pass a pointer type to sizeof(), it tells you the size of the pointer, not the size of the data structure that the pointer points to. That's not your problem.
I haven't looked over your code in enough detail to tell what IS wrong with it. I find it easier to troubleshoot code by stepping through it in the debugger. That would be my suggestion.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
If you pass a pointer type to sizeof(), it tells you the size of the pointer, not the size of the data structure that the pointer points to. That's not your problem.
So if this NSLog is representing the imageData array wrongly, how do I log the array size correctly?
Also, I tried stepping through it with the debugger. Nothing jumps out, since these are Core Graphics opaque types. But since nothing is being created in the touches___ methods, I think the cause is probably in drawRect.
Additionally, the imageData shows up in the debugger as
imgData = (unsigned char *) 0xdc5c020
*imgData = (unsigned char) 0 '\000'
Thanks for your reply!
Last edited by architectpianist; 12-26-2011 at 08:06 PM.
By repetitive logging I found that the size of the bitmapData is in fact in the 300,000s, which is the range it should be in for a 280 x 280 image with 4 bytes per pixel. Then I added this at the end of the drawRect method, just to check:
Nothing shows up on the screen when I do this! So it comes down to currentColorWheel being empty, but not NULL.
I've changed the creation of currentColorWheel from