【发布时间】: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