Comprobación de UIImage Pixel by Pixel

Estoy creando una aplicación en la que trato de get el color del punto actual tocado, si este punto no es negro, entonces ejecutará una iteración / recursión y almacenará todos los puntos no negros en la matriz. Estoy usando esta function para Iteración:

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

Ahora, estoy cambiando el color de esos puntos que están en matriz, pero solo los colors de la línea recta de puntos, no cada píxel. Cualquier ayuda será muy apreciada. ¡Gracias!

Prueba esto. No puedo probarlo, pero debería funcionar.

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

También elimine estas variables de su class canDoPositiveX, canDoPositiveY, canDoNegativeX, canDoNegativeY

Comtesting esta pregunta:

¿Cómo get datos de píxeles de un UIImage (Cocoa Touch) o CGImage (Core Graphics)?

Debería poder usar / modificar fácilmente el código en la respuesta aceptada para devolver solo las coorderadas de píxeles negras (o casi negras) para un UIImage dado.

Pruebe con este código modificado:

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