Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if CGPoint is within image area

I'm trying to figure out whether a CGPoint lies within the shape of an image. The image is a simple black shape such as the two attached below. I'd like to create a method that determines whether or not a CGPoint lies within the black area of that shape.

I think this needs two things: 1) Turning the image into something that can be read with code (not sure what kind of image processing this would use or how)

2) Using that as a reference to determine whether or not a CGPoint lies within it.

Any help or ideas appreciated. I've never done image processing type of coding before. Thanks!

enter image description here

like image 974
user339946 Avatar asked Mar 04 '12 10:03

user339946


1 Answers

Take a look at Ole Begemann's OBShapedButton. There you'll find an UIImage category that contains ColorAtPixel method. Guess that's what you're looking for.

You can then get UIColor of certain pixel with:

UIImage *image = [UIImage imageWithCGImage:yourCGImage];
CGPoint point = CGPointMake(pointx,pointy);
UIColor *pixelColor = [image colorAtPixel:point];

To simplify getting RGB values you could also take a look at uicolor-utilities. Using UIColor-Expanded category you can simply determine let's say red and blue component. If they are low (pixel is dark) then tested point is inside shape.

CGFloat redComp = [pixelColor red];
CGFloat blueComp = [pixelColor blue];

BOOL isInsideShape = ((redComp < 0.5) && (blueComp < 0.5));
like image 194
Rok Jarc Avatar answered Oct 04 '22 16:10

Rok Jarc