2013-03-26 16 views
8

sto creando un'applicazione in cui sto cercando di ottenere il colore del punto toccato corrente, se questo punto non è nero allora eseguirà un'iterazione/ricorsione e memorizzerà tutti i punti non neri in array. Sto usando questa funzione per l'iterazione:Controllo pixel UIImage per pixel

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

} 

Ora, sto cambiando il colore di quei punti che sono in ordine, ma solo i colori linea retta di punti, non tutti i pixel. Qualsiasi aiuto sarà molto apprezzato. Grazie!

+0

è che la sintassi corretta per un metodo? Non sembra giusto per me. Hai chiamato la tua funzione "funzione"? Non molto esplicito. Cosa sono canDoNegativeY e canDoNegativeX? – Odrakir

+0

Sono solo assegni (booleani). Inoltre, non ho idea di questa ricorrenza di tipo f: P. Se sai come farlo bene, gentilmente dimmelo. @Odrakir Posso darti generosità del valore di 100 per quello. –

+1

So che sono booleani, ma temo di non poterti aiutare se non capisco cosa fa il tuo codice. Cosa significano quei booleani? – Odrakir

risposta

2

Prova questo. Non riesco a testarlo, ma dovrebbe funzionare.

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

anche rimuovere queste variabili dalla classe canDoPositiveX, canDoPositiveY, canDoNegativeX, canDoNegativeY

+0

Grazie per il tuo aiuto. Penso che tu non abbia ottenuto quello che voglio fare. Fammi chiarire prima questo. Ho una UIImage, quando l'utente tocca la parte non nera di UIImage, dovrebbe iterare e salvare tutti i punti non neri di UIImage e poiché l'iterazione ha trovato il punto nero che dovrebbe fermarsi. Ma questo codice che mi hai dato non funziona. –

2

prova con questo codice modificato:

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

Ciao, grazie per la tua risposta. Ho già provato questo codice. Ma in questo codice il problema è che causa uno stack overflow, come quando ho provato a chiamare questa funzione, la mia applicazione è stata messa in pausa e improvvisamente si è bloccata :( –