提问人:Brittany 提问时间:7/29/2020 最后编辑:Brittany 更新时间:7/30/2020 访问量:43
Obj-C - 点击手势以返回 imageview 中的 RGB 颜色返回不正确的颜色?
Obj-C - Tap gesture to return RGB color in imageview returning incorrect color?
问:
我使用下面的代码来获取在图像视图中点击的点的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();
}
答:
1赞
Ol Sen
7/29/2020
#1
在 self.view
中使用 CGPoint
从 UIPanGestureRecognizer
中选择具有正确颜色空间
的 UIColor
@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];
}
甚至比以前的解决方案更简单,玩得开心。
评论
0赞
Brittany
7/30/2020
奇怪的是,这似乎行不通。无论我点击色轮的哪个位置,颜色都会歪斜。例如,如果我点击滚轮中心的纯灰色,则返回 RGB 值 0,0,0。如果我点击一种颜色,返回的 3 个值总是返回 255 作为值之一。不知道发生了什么:/
0赞
Ol Sen
7/30/2020
编辑,所以你可以看到色彩空间和坐标很重要。
0赞
Brittany
7/30/2020
哦,这很有趣哈哈!非常感谢,这对:)非常有帮助
0赞
Brittany
7/30/2020
这很有效;也就是说,如果我将 panGesture 中的内容粘贴到我的 tapGesture 中,我怎样才能从中提取实际的 RGB 值?显示颜色本身有效,但我似乎无法提取值以供使用 :/原谅我的新手哈哈
0赞
Ol Sen
7/30/2020
这是因为您的代码需要通用的 RGBA,但视图的格式最好使用 kCGImageAlphaPremultipliedLast 完成,并且点的坐标与 CGContext 坐标不匹配。换句话说,我们把:D弄得一团糟
评论