【问题标题】:Obj-C - Tap gesture to return RGB color in imageview returning incorrect color?Obj-C - 点击手势以在图像视图中返回 RGB 颜色返回不正确的颜色?
【发布时间】:2020-07-28 19:41:20
【问题描述】:

我正在使用下面的代码来获取图像视图中点击的点的 RGB 值(图像视图是一个色轮)。点击图像效果很好,并返回一个 RGB 值 - 但它不是正确的颜色。例如,我点击滚轮的黄色区域,会返回一个红色的 RGB 值。有谁知道我错过了什么/为什么会发生这种情况?

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self.colorWheel addGestureRecognizer:tapRecognizer];
    self.colorWheel.userInteractionEnabled = YES;
 
    
}

- (void)tapGesture:(UITapGestureRecognizer *)recognizer
{
    CGPoint point1 = [recognizer locationInView:recognizer.view];

    UIGraphicsBeginImageContext(recognizer.view.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [recognizer.view.layer renderInContext:context];

    int bpr = (int)CGBitmapContextGetBytesPerRow(context);
    unsigned char * data = CGBitmapContextGetData(context);
  
    if (data != NULL)
        
    {
        int offset = bpr*round(point1.y) + 4*round(point1.x);
        int red = data[offset+0];
        int green = data[offset+1];
        int blue = data[offset+2];

        NSLog(@"%d %d %d", red, green, blue);
        
      
    }

    UIGraphicsEndImageContext();
}

【问题讨论】:

    标签: objective-c uiimageview uitapgesturerecognizer


    【解决方案1】:

    self.view 中选择UIColorCGPoint,从UIPanGestureRecognizer 中选择正确的colorspace

    @property (nonatomic) UIView *colorView; //preview of chosen colors
    

    在实现中设置一个识别器和提供我们将从中选择的颜色的图像

    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    //pan.maximumNumberOfTouches = 1;
    UIImage *img = [UIImage imageNamed:@"image_overlap"]; //an Assets image
    UIImageView *colorWheel = [[UIImageView alloc] initWithImage:img];
    colorWheel.frame = self.view.frame;
    colorWheel.userInteractionEnabled = YES;
    [colorWheel addGestureRecognizer:pan];
    [self.view addSubview:colorWheel];
    
    _colorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [self.view addSubview:_colorView];
    

    捕捉手势位置并使用所选颜色设置预览颜色。

    - (void)panGesture:(UIPanGestureRecognizer *)recognizer {
        CGPoint location = [recognizer locationInView:recognizer.view];
        CGPoint p = { round(location.x), round(location.y) };
        _colorView.backgroundColor = [self colorInViewAtPoint:p];
    }
    

    在 self.view 中查找颜色

    -(UIColor *)colorInViewAtPoint:(CGPoint)p {
        unsigned char pixel[4] = {0};
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
        // correct panlocation vs bitmapt coordintes
        CGContextTranslateCTM(context, -p.x, -p.y);
        [self.view.layer renderInContext:context];
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        return [UIColor colorWithRed:pixel[0]/255.0 
                               green:pixel[1]/255.0 
                                blue:pixel[2]/255.0 
                               alpha:pixel[3]/255.0];
    }
    

    比以前的解决方案更简单,玩得开心。

    【讨论】:

    • 奇怪的是,这似乎不起作用。无论我点击色轮的哪个位置,颜色都会偏斜。例如,如果我点击滚轮中心的纯灰色,则会返回 RGB 值 0,0,0。如果我点击一种颜色,则返回的 3 个值总是返回 255 作为其中一个值。不知道发生了什么:/
    • 已编辑,因此您会看到色彩空间和坐标很重要。
    • 哦,这很有趣哈哈!非常感谢,这很有帮助:)
    • 效果很好;也就是说,如果我将 panGesture 中的内容粘贴到我的 tapGesture 中,我如何从中提取实际的 RGB 值?显示颜色本身是有效的,但我似乎无法提取要使用的值:/ 请原谅我的新手 lol
    • 这是因为您的代码需要通用 rgba,但视图布局上下文的格式最好使用 kCGImageAlphaPremultipliedLast 完成,并且点的坐标与 CGContext 坐标不匹配。换句话说,我们弄得一团糟:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    相关资源
    最近更新 更多