Obj-C - 在 imageview 中点按 return RGB 颜色 return 颜色不正确?

Obj-C - Tap gesture to return RGB color in imageview returning incorrect color?

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

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

self.view 中选择 UIColor,从 UIPanGestureRecognizer 中选择 CGPoint,正确 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];
}

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