通过像素检查UIImage像素

时间:2013-03-26 09:22:13

标签: iphone ios objective-c

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

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

}

现在,我正在改变阵列中那些点的颜色,但它只会对点的直线进行颜色处理,而不是每个像素。任何帮助将非常感激。谢谢!

3 个答案:

答案 0 :(得分: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

中删除这些变量

答案 1 :(得分:2)

检查此问题:

How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?

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

答案 2 :(得分: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]);
}