1

コードを使用して円形グラデーションをレンダリングし、タッチでグラデーションに十字線 (サブビュー) を作成しています。タッチ位置のピクセルを読み取ってRGB値を返したいのですが、常に同じ値が返されます..

EDIT:グラデーションをレンダリングするコードを追加しました

完全に新しいコード:

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 はこれだけ遅いのでしょうか、それとも私のフレーム バッファに何か問題がありますか?

4

2 に答える 2

1

イベントが検出されるたびに、ジェスチャ認識エンジンの翻訳をリセットしています。これは、 の座標colorPointがあまり変化しないことを意味します。

colorPointこれを使っての座標系sender.view.centerに変換して計算する必要があります。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];

NowcolorPointは、ジェスチャ レコグナイザーのビューの現在の位置であり、ivの座標系で表されます。

于 2013-07-05T10:00:48.180 に答える
0

よし、更新されない問題は自分で解決した。私は今、まったく異なる読書方法を使用しています。

センタリングを始めてくれた Nikolai に感謝

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







}
于 2013-07-08T10:28:44.637 に答える