简体   繁体   English

如何获取返回rgb,十六进制值的图像像素? IOS

[英]How to get pixels of an image which return rgb,hex value? ios

I am new to ios so please be gentle... 我是ios的新手所以请温柔......

I have an imageview which display selected image from the photo library . 我有一个imageview ,显示photo library所选图像。

I want to pick the color which i select touching on the image with its rgb and hex value. 我想选择我用rgbhex值选择触摸图像的颜色。

I think you should try this method: 我想你应该试试这个方法:

- (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];
}

or try this one: 或尝试这个:

http://www.markj.net/iphone-uiimage-pixel-color/ http://www.markj.net/iphone-uiimage-pixel-color/

This is pretty cool approach. 这是非常酷的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM