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!
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With