从像素检索的RGB值不正确

时间:2013-07-08 10:31:45

标签: ios objective-c rgb

我渲染了一个圆形渐变,并创建了一种方法,让我用手指扫过它,使用平移手势识别器。

我正在检索当前触摸位置的像素,并希望检索它的颜色。

这意味着,在渐变上移动时,颜色值应不断更新。

我正在使用以下代码:

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







}

这不能按预期工作,给我错误的颜色,并且根本没有显示某些颜色(如红色)

编辑:由于低代表,我无法添加图片。我现在将添加用于渲染渐变的代码

代码:

- (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, size.height)/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];

}

1 个答案:

答案 0 :(得分:0)

这里的第一个问题是你计算colorPoint的方式。它们现在是这样,colorPoint将始终是视图的中心点。这个handlePan:方法应该让您了解视图中的最后一次触摸:

- (IBAction)handlePan:(UIPanGestureRecognizer *)sender
{
    if (sender.numberOfTouches)
    {
        CGPoint lastPoint = [sender locationOfTouch: sender.numberOfTouches - 1 inView: sender.view];
        NSLog(@"lastPoint: %@", NSStringFromCGPoint(lastPoint));
    }
}

从那里,我可能会建议不要将图像blitting到位图上下文中,然后尝试从那里读回来,你只需使用你用来创建的相同数学过程来计算点的颜色首先是图像。你现在这样做的方式将是CPU +内存密集型。

编辑:这就是我想出的。这个对我有用。从Xcode中的单视图应用程序模板开始,并使用您发布的代码,我在ViewController.m中有以下代码:

@implementation ViewController
{
    UIImage* img;
    UIImageView* iv;
}

- (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, size.height)/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];
    colorView = [[UIView alloc] init];
    colorView.frame = CGRectMake(CGRectGetMaxX(bounds) - 25, CGRectGetMaxY(bounds) - 25, 20, 20);
    [self.view addSubview:colorView];

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(handlePan:)];

    [self.view addGestureRecognizer: panGesture];    
}

- (IBAction)handlePan:(UIPanGestureRecognizer *)sender
{
    if (sender.numberOfTouches)
    {
        CGPoint lastPoint = [sender locationOfTouch: sender.numberOfTouches - 1 inView: sender.view];
        CGRect bounds = self.view.bounds;
        CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
        CGPoint delta = CGPointMake(lastPoint.x - center.x,  lastPoint.y - center.y);
        CGFloat angle = (delta.y == 0 ? delta.x >= 0 ? 0 : M_PI : atan2(delta.y, delta.x));
        angle = fmod(angle,  M_PI * 2.0);
        angle += angle >= 0 ? 0 : M_PI * 2.0;
        UIColor *color = [UIColor colorWithHue: angle / (M_PI * 2.0) saturation:1. brightness:1. alpha:1];
        colorView.backgroundColor = color;
        CGFloat r,g,b,a;
        if ([color getRed: &r green: &g blue:&b alpha: &a])
        {
            NSLog(@"Color value - R : %g G : %g : B %g", r, g, b);
        }
    }
}

@end

我看到的是,当我将手指拖过渐变时,我得到一个稳定的消息流来控制台,其RGB值对应于我手指的位置。我还添加了代码以在右下角的小视图中显示最后一种颜色。它也不使用位图上下文。希望这会有所帮助。