2015-11-08 1 views
-2

Pour mon application, j'ai besoin d'implémenter un espace de dessin. Avec l'aide d'un tutoriel en ligne j'ai réussi à dessiner. Mais le défi consiste maintenant à dessiner avec des couleurs différentes. J'ai écrit le code ci-dessous, mais cela ne fonctionne pas et je ne comprends pas pourquoi cela ne fonctionne pas. Il peint juste noir. Je donne les résultats suivants dans mon UIView.mCouleurs pour l'application de dessin

- (void)drawBitmap 
{ 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0); 

    if (!incrementalImage) // first time; paint background white 
    { 
     UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; 
     [[UIColor whiteColor] setFill]; 
     [rectpath fill]; 
    } 
    [incrementalImage drawAtPoint:CGPointZero]; 
    [color setStroke]; 
    [path stroke]; 
    incrementalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 

- (void)color1 { 
    color = [UIColor greenColor]; 
} 

- (void)color2 { 
    color = [UIColor yellowColor]; 
} 

j'appelle les fonctions de mon ViewController.m

- (IBAction)drawGreen:(id)sender { 
    SmoothedBIView *smoothView = [[SmoothedBIView alloc]init]; 
    [smoothView color1]; 
} 

- (IBAction)drawYellow:(id)sender { 
    SmoothedBIView *smoothView = [[SmoothedBIView alloc] init]; 
    [smoothView color2]; 
} 

Merci pour votre aide!

+0

Qu'est-ce que « ça ne marche pas » signifie? Quel est le résultat attendu et quel est le résultat? –

+0

La couleur est juste noire, elle ne change pas pour une autre couleur. – user3638160

+0

Vous n'avez pas montré comment vos fonctions color1 et color2 sont appelées. – Craig

Répondre

0

Je soupçonne que même si vous créez un contexte bitmap vous ne dessinez pas en propre .. essayer ce

- (void)drawBitmap 
    { 

     CGContextRef ctx = UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);//changed 

     UIGraphicsPushContext(ctx);//changed 

     if (!incrementalImage) // first time; paint background white 
     { 
      UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; 
      [[UIColor whiteColor] setFill]; 
      [rectpath fill]; 
     } 
     [incrementalImage drawAtPoint:CGPointZero]; 
     [color setStroke]; 
     [path stroke]; 
     incrementalImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsPopContext(ctx); //changed 

     UIGraphicsEndImageContext(); 
    } 
+0

Initialiser 'CGContextRef' (alias 'struct CGContext *') avec une expression de type incompatible 'void', est-ce que je fais quelque chose de mal? – user3638160