Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios glReadPixels()未更新_Ios_Objective C_For Loop_Opengl Es - Fatal编程技术网

Ios glReadPixels()未更新

Ios glReadPixels()未更新,ios,objective-c,for-loop,opengl-es,Ios,Objective C,For Loop,Opengl Es,我正在使用代码渲染圆形渐变,并通过触摸在渐变上创建十字线(子视图)。我现在想读取触摸位置的像素,让它返回RGB值,但它总是给我相同的值 编辑:添加渲染渐变的代码 全新代码: viewDidLoad - (void)viewDidLoad { [super viewDidLoad]; CGSize size = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);

我正在使用代码渲染圆形渐变,并通过触摸在渐变上创建十字线(子视图)。我现在想读取触摸位置的像素,让它返回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];

    }
一些NSLog:

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
是手势识别器视图的当前位置,以
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];
您应该使用
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
是手势识别器视图的当前位置,以
iv
的坐标系表示。

好的,它不更新的问题由我自己解决。
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];
我现在用的是一种完全不同的阅读方法

感谢尼古拉让我开始做定心

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


    CGPoint translation = [sender translationInView:iv];
    [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];





    CGImageRef image = img.CGImage;
    NSUInteger width = CGImageGetWidth(image);
    NSUInteger height = CGImageGetHeight(image);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    char *rawData = malloc(height * width * 4);
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(
                                                 rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
                                                 );
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
    CGContextRelease(context);
    int byteIndex = (bytesPerRow * colorPoint.y) + colorPoint.x * bytesPerPixel;
    unsigned char red = rawData[byteIndex];
    unsigned char green = rawData[byteIndex+1];
    unsigned char blue = rawData[byteIndex+2];


    UIColor *hauptfarbe = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    ch.backgroundColor = hauptfarbe;





    NSLog(@"Color value - R : %i G : %i : B %i",red, green, blue);







}

好吧,it不更新的问题我自己解决了。 我现在用的是一种完全不同的阅读方法

感谢尼古拉让我开始做定心

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


    CGPoint translation = [sender translationInView:iv];
    [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];





    CGImageRef image = img.CGImage;
    NSUInteger width = CGImageGetWidth(image);
    NSUInteger height = CGImageGetHeight(image);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    char *rawData = malloc(height * width * 4);
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(
                                                 rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
                                                 );
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
    CGContextRelease(context);
    int byteIndex = (bytesPerRow * colorPoint.y) + colorPoint.x * bytesPerPixel;
    unsigned char red = rawData[byteIndex];
    unsigned char green = rawData[byteIndex+1];
    unsigned char blue = rawData[byteIndex+2];


    UIColor *hauptfarbe = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
    ch.backgroundColor = hauptfarbe;





    NSLog(@"Color value - R : %i G : %i : B %i",red, green, blue);







}

坐标现在正在正确更新,但颜色没有更新。还有一件事:永远不要直接操纵要移动或旋转的视图;更确切地说,是它的超视图。坐标现在正在正确更新,但颜色没有更新。还有一件事:永远不要直接操纵你试图移动或旋转的视图;更确切地说,是它的超视界。我还没弄明白,悲哀的是,我还没弄明白