我是iOS的新手,请温柔一点...
我有一个imageview
,它显示了从照片库
中选择的图像。
我想要选择颜色,通过触摸图像来获取其RGB
和Hex
值。
我是iOS的新手,请温柔一点...
我有一个imageview
,它显示了从照片库
中选择的图像。
我想要选择颜色,通过触摸图像来获取其RGB
和Hex
值。
- (UIColor *)colorAtPosition:(CGPoint)position
{
CGRect sourceRect = CGRectMake(position.x, position.y, 1.f, 1.f);
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, sourceRect);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *buffer = malloc(4);
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big;
CGContextRef context = CGBitmapContextCreate(buffer, 1, 1, 8, 4, colorSpace, bitmapInfo);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0.f, 0.f, 1.f, 1.f), imageRef);
CGImageRelease(imageRef);
CGContextRelease(context);
CGFloat r = buffer[0] / 255.f;
CGFloat g = buffer[1] / 255.f;
CGFloat b = buffer[2] / 255.f;
CGFloat a = buffer[3] / 255.f;
free(buffer);
return [UIColor colorWithRed:r green:g blue:b alpha:a];
}
或者试试这个:
http://www.markj.net/iphone-uiimage-pixel-color/
这是一个相当不错的方法。