从图像中获取颜色

时间:2012-02-15 05:04:39

标签: iphone objective-c

我是ios的新手 我正在做一个应用程序,因为我得到了使用触摸事件从图像中获取颜色的要求,只是显示用户在图像上触摸的颜色的警告

1 个答案:

答案 0 :(得分:3)

@implementation UIView (ColorOfPoint)

- (UIColor *) colorOfPoint:(CGPoint)point
{
      unsigned char pixel[4] = {0};
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
      CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);
      CGContextTranslateCTM(context, -point.x, -point.y);
      [self.layer renderInContext:context];
      CGContextRelease(context);
      CGColorSpaceRelease(colorSpace);
      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;
}
@end

添加此功能作为UIView的扩展。将点(x和y坐标)传递给它,它将返回一种颜色。

相关问题