8

我正在创建一个应用程序,我试图在其中获取当前触摸点的颜色,如果该点不是黑色,那么它将运行迭代/递归并将所有非黑色点存储在数组中。我正在使用这个函数进行迭代:

-(void)function:(CGFloat)positiveX:(CGFloat)positiveY:(CGFloat)negativeX:(CGFloat)negativeY
{
    if (canDoPositiveX == YES)
    {
        //Checking in positive
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(positiveX, positiveY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX+1 :positiveY :negativeX :negativeY];
        }
        else
        {
            canDoPositiveX = NO;
        }
    }

    if (canDoPositiveY == YES)
    {
        //Checking in positive
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 0"])
        {
            CGPoint point = CGPointMake(positiveX, positiveY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY+1 :negativeX :negativeY];
        }
        else
        {
            canDoPositiveY = NO;
        }
    }

    if (canDoNegativeX == YES)
    {
        //Checking in negative
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(negativeX, negativeY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(negativeX, negativeY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY :negativeX-1 :negativeY];
        }
        else
        {
            canDoNegativeX = NO;
        }
    }

    if (canDoNegativeY == YES)
    {
        //Checking in negative        
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(negativeX, negativeY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            negativeY -= 1;
            CGPoint point = CGPointMake(negativeX, negativeY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY :negativeX :negativeY-1];
        }
        else
        {
            canDoNegativeY = NO;
        }
    }

    NSLog(@"%u",[array count]);

}

现在,我正在更改数组中那些点的颜色,但它只为点的直线着色,而不是每个像素。任何帮助将不胜感激。谢谢!

4

3 回答 3

2

检查这个问题:

如何从 UIImage (Cocoa Touch) 或 CGImage (Core Graphics) 获取像素数据?

您应该能够轻松地使用/修改已接受答案上的代码,以仅返回给定的黑色(或接近黑色)像素坐标UIImage

于 2013-04-17T23:33:45.253 回答
2

尝试这个。我实际上无法测试它,但它应该可以工作。

-(void)function:(CGFloat)positiveX:(CGFloat)positiveY:(CGFloat)negativeX:(CGFloat)negativeY
{
    // Check your current point 
    if ([[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
    {
        // reached the dead end (break recursion)
        return;
    }

    CGPoint point = CGPointMake(positiveX, positiveY);
    [array addObject:[NSValue valueWithCGPoint:point]];

    BOOL canDoPositiveX = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX+1, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];
    BOOL canDoPositiveY = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY+1)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];
    BOOL canDoNegativeX = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX-1, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];
    BOOL canDoNegativeY = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY-1)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];

    if (canDoPositiveX == YES)
    {
        [self function:positiveX+1 :positiveY :negativeX :negativeY];
    }

    if (canDoPositiveY == YES)
    {
        [self function:positiveX :positiveY+1 :negativeX :negativeY];
    }

    if (canDoNegativeX == YES)
    {
        [self function:positiveX :positiveY :negativeX-1 :negativeY];
    }

    if (canDoNegativeY == YES)
    {
        [self function:positiveX :positiveY :negativeX :negativeY-1];
    }

    NSLog(@"%u",[array count]);
}

还要从你的类 canDoPositiveX、canDoPositiveY、canDoNegativeX、canDoNegativeY 中删除这些变量

于 2013-03-26T09:42:53.033 回答
2

尝试使用此修改后的代码:

-(void)function:(CGFloat)positiveX:(CGFloat)positiveY:(CGFloat)negativeX:(CGFloat)negativeY
{
    if (canDoPositiveX == YES)
    {
        //Checking in positive
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(positiveX, positiveY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX+1 :positiveY :negativeX :negativeY];
        }
        else
        {
            canDoPositiveX = NO;
            canDoPositiveY = YES;
            canDoNegativeX = YES;
            canDoNegativeY = YES;
        }
    }

    if (canDoPositiveY == YES)
    {
        //Checking in positive
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 0"])
        {
            CGPoint point = CGPointMake(positiveX, positiveY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY+1 :negativeX :negativeY];
        }
        else
        {
            canDoPositiveX = YES;
            canDoPositiveY = NO;
            canDoNegativeX = YES;
            canDoNegativeY = YES;
        }
    }

    if (canDoNegativeX == YES)
    {
        //Checking in negative
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(negativeX, negativeY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(negativeX, negativeY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY :negativeX-1 :negativeY];
        }
        else
        {
            canDoPositiveX = YES;
            canDoPositiveY = YES;
            canDoNegativeX = NO;
            canDoNegativeY = YES;
        }
    }

    if (canDoNegativeY == YES)
    {
        //Checking in negative        
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(negativeX, negativeY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(negativeX, negativeY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY :negativeX :negativeY-1];
        }
        else
        {
            canDoPositiveX = YES;
            canDoPositiveY = YES;
            canDoNegativeX = YES;
            canDoNegativeY = NO;
        }
    }

    NSLog(@"%u",[array count]);
}
于 2013-04-20T17:28:14.843 回答