简体   繁体   中英

Checking UIImage Pixel by Pixel

i am creating an application in which i am trying to get the color of current touched point, if this point is not black then it will run an iteration/recursion and will store all the non black points in array. I am using this function for Iteration:

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

}

Now, i am changing the color of those points which are in array, But it only colors straight line of points, not every pixel. Any help will be really appreciated. Thanks!

Try this. I can't actually test it but it should work.

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

Also remove these variables from your class canDoPositiveX, canDoPositiveY, canDoNegativeX, canDoNegativeY

Check this question:

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

You should be able to easily use/modify the code on the accepted answer to return only the black (or nearly black) pixels coordinates for a given UIImage .

Try with this modified code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM