I am trying to develop a small application. In it, I need to detect the color of a pixel within an UIView. I have a CGPoint defining the pixel I need. The colors of the UIView are changed using CoreAnimation.
I know there are some complex ways to extract color information from UIImages. However I couldn't find a solution for UIViews.
In Pseudo-Code I am looking for something like
pixel = [view getPixelAtPoint:myPoint];
UIColor *mycolor = [pixel getColor];
Any input greatly appreciated.
Here is more efficient solution:
Link to files: https://github.com/ivanzoid/ikit/tree/master/UIView+ColorOfPoint
A swift'ified version of ivanzoid's answer
Swift 3
Swift 2
based around the underlying
CALayer
but easily translatable back toUIView
.It is pretty horrible and slow. Basically you create a bitmap context with a backing store you allocate so you can read the memory, then you render the views layer in the context and read the appropriate point in ram.
If you know how to do it for an image already you can do something like this:
And then you will get an image where you can get the pixel data. I am sure the mechanism you have for dealing with images involves rendering them into a context anyway, so you can merge that with this and factor out the actual image creation. So if you take that could and remove the bit where you load the image and replace it with the context render:
you should be good.
Fixed some minor errors
Also, add
to your file to avoid warning messages.
Combining your code with the code found here works flawlessly.