carya.liu carya.liu - 1 month ago 20
iOS Question

How to get the color of a pixel in an UIView

The color circle can rotate, when the circle rotate, and I want to get the color at triangle of the color circle´╝îI used the method :

- (UIColor *) colorOfPoint:(CGPoint)point
{
unsigned char pixel[4] = {0};

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);

CGContextTranslateCTM(context, -point.x, -point.y);

[self.layer renderInContext:context];

CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

//NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]);

UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];

return color;
}


But It not get the correct color when circle rotate, I all code is on the github:
https://github.com/carya/GetViewColor , can any one help me to fix this bug, thanks so much.

enter image description here

PS: I have test the - (UIColor *)colorOfPoint:(CGPoint)point method is right at normal view, but in my code, the result looks strange.




Update:

Finally I fixed this problem and post an article to explain this, you can reach at this

Answer

Finally I fixed this problem and post an article to explain this, you can reach at this