如何获得一个像素的颜色在UIView的
问题描述:
彩色圆圈可以旋转,当圆旋转,我想在色环的三角形得到的颜色,我用的方法:如何获得一个像素的颜色在UIView的
- (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;
}
但是当圆圈旋转时它没有得到正确的颜色,我所有的代码都在github上: https://github.com/carya/GetViewColor,任何人都可以帮我修复这个bug,非常感谢。
PS:我有测试 - (*的UIColor)colorOfPoint:(CGPoint)点的方法是正确的,在普通视图,但在我的代码,结果看起来很奇怪。
更新:
最后我解决了这个问题,并发表了一篇文章,解释这一点,你可以达到在this
你是如何创建该色环? – Fogmeister 2014-11-06 14:20:09
@Fogmeister,我使用UIBezierPath和CAShapeLayer来创建色环,你可以在https://github.com/carya/GetViewColor/blob/master/GetViewColor/DrawCircus.m看到DrawCircus类的代码,请帮助我解决这个问题,谢谢。 – 2014-11-06 14:27:39
啊,它来自图像。以为你可能画了蓝色和黄色。 – Fogmeister 2014-11-06 14:36:54