gpt4 book ai didi

ios - glReadPixels() 不更新

转载 作者:行者123 更新时间:2023-11-29 13:10:36 27 4
gpt4 key购买 nike

我正在使用代码渲染圆形渐变并通过触摸在渐变上创建十字准线( subview )。我现在想读取触摸位置的像素并让它返回 RGB 值,但它总是给我相同的值..

编辑:添加了呈现渐变的代码

全新代码:

viewDidLoad

 - (void)viewDidLoad
{
[super viewDidLoad];
CGSize size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), YES, 0.0);
[[UIColor whiteColor] setFill];
UIRectFill(CGRectMake(0, 0, size.width, size.height));

int sectors = 180;
float radius = MIN((size.width - 100), (size.height - 100))/2;
float angle = 2 * M_PI/sectors;
UIBezierPath *bezierPath;
for ( int i = 0; i < sectors; i++)
{
CGPoint center = CGPointMake((size.width/2), (size.height/2));
bezierPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:i * angle endAngle:(i + 1) * angle clockwise:YES];
[bezierPath addLineToPoint:center];
[bezierPath closePath];
UIColor *color = [UIColor colorWithHue:((float)i)/sectors saturation:1. brightness:1. alpha:1];
[color setFill];
[color setStroke];
[bezierPath fill];
[bezierPath stroke];
}
img = UIGraphicsGetImageFromCurrentImageContext();
iv = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:iv];
[self.view addSubview:ch];

}

我的平移手势识别器:

    - (IBAction)handlePan:(UIPanGestureRecognizer *)sender {


CGPoint translation = [sender translationInView:self.view];
[sender setTranslation:CGPointZero inView:self.view];

CGPoint center = sender.view.center;
center.x += translation.x;
center.y += translation.y;

sender.view.center = center;
CGPoint colorPoint = [sender.view.superview convertPoint:center toView:iv];

[sender setTranslation:CGPointMake(0, 0) inView:self.view];


CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(img.CGImage));
const UInt8* data = CFDataGetBytePtr(pixelData);

int pixelInfo = (img.size.width * colorPoint.y ) +colorPoint.x ;

float red = data[pixelInfo];
float green = data[(pixelInfo + 1)];
float blue = data[pixelInfo + 2];
float alpha = data[pixelInfo + 3];
UIColor *pixelcolor = [UIColor colorWithRed:red/255 green:green/255 blue:blue/255 alpha:alpha]; // The pixel color info
CFRelease(pixelData);
NSLog(@"Color Value : %f, %f, %f, %f",red,green,blue,alpha);


}

一些 NSLogs :

2013-07-05 10:04:20.913 ColorPicker[614:11603] pixel color: 156, 212, 255
2013-07-05 10:04:20.929 ColorPicker[614:11603] pixel color: 156, 212, 255
2013-07-05 10:04:20.947 ColorPicker[614:11603] pixel color: 156, 212, 255
2013-07-05 10:04:21.014 ColorPicker[614:11603] pixel color: 156, 212, 255
2013-07-05 10:04:21.047 ColorPicker[614:11603] pixel color: 156, 212, 255
2013-07-05 10:04:21.447 ColorPicker[614:11603] pixel color: 156, 212, 255

编辑:颜色也不正确。我在第一步得到了不同的 RGB 值,然后值改变一次并保持不变。

是 glReadPixels 这么慢,还是我的帧缓冲区有问题?

最佳答案

您将在每个检测到的事件后重置手势识别器的翻译。这意味着 colorPoint 中的坐标变化不大。

您应该使用 sender.view.center 并将其转换为 iv 的坐标系来计算 colorPoint

CGPoint translation = [sender translationInView:self.view];
[sender setTranslation:CGPointZero inView:self.view];

CGPoint center = sender.view.center;
center.x += translation.x;
center.y += translation.y;

sender.view.center = center;
CGPoint colorPoint = [sender.view.superview convertPoint:center toView:iv];

现在 colorPoint 是手势识别器 View 的当前位置,以 iv 的坐标系表示。

关于ios - glReadPixels() 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17483504/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com