2009-12-22 72 views
1

畫我想畫nil,而不是顏色的,因爲最終我想要的代碼,以便您可以看到什麼是UIImageView背後擦除UIImageView的部分。如何使用零CGContext上

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    currentPoint.x = currentPoint.x; 
    currentPoint.y = currentPoint.y; 
    mouseSwiped = YES; 
    UIGraphicsBeginImageContext(self.view.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES); 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal); 
    CGContextSetAlpha(UIGraphicsGetCurrentContext(), drawingColorAlpha); 
    CGContextSaveGState(UIGraphicsGetCurrentContext()); //Probably right here 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 
      drawingColorRed, // I want to draw nil here instead of these CGFloats that I have made 
      drawingColorGreen, // I want to draw nil here instead of these CGFloats that I have made 
      drawingColorBlue, // I want to draw nil here instead of these CGFloats that I have made 
      drawingColorAlpha); // I want to draw nil here instead of these CGFloats that I have made 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 
              lastPoint.x, 
              lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 
              currentPoint.x, 
              currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing my context to 
    NSLog(@"current point x: %d current point y: %d",currentPoint.x, currentPoint.y); //Used for my purposes 
    lastPoint = currentPoint; 
    mouseMoved++; 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if (!optionsDisplayerVisible && canDraw) 
    { 
     if(!mouseSwiped) 
     { 
      UIGraphicsBeginImageContext(self.view.frame.size); 
      [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
      CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES); 
      CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSizeVal); 
      CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 
             drawingColorRed, 
             drawingColorGreen, 
             drawingColorBlue, 
             drawingColorAlpha); 
      CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
      CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
      CGContextStrokePath(UIGraphicsGetCurrentContext()); 
      CGContextFlush(UIGraphicsGetCurrentContext()); 
      drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); //drawImage is the UIImageView that I am drawing to 
      UIGraphicsEndImageContext(); 
     } 
    } 
} 

回答

1

Colin Gislason的回答是錯誤的。如果將drawingColorAlpha設置爲0,則只會在現有圖像上繪製不可見的顏色。

這是什麼工作。

//set this at the beginning 
CGContextSetLineCap(ctx,kCGImageAlphaNone); 
//after moving the point to your pen location: 
CGContextClearRect (ctx, CGRectMake(lastPoint.x, lastPoint.y, 10, 10)); 
-1

你想要的是不爲零,但顏色是透明的。如果您的UIImageView在IB中未標記爲「不透明」,則應該在像素透明的任何區域顯示。

+0

不,實際上我想要它清除UIImageView的一部分 – Jaba 2009-12-22 21:47:26

+1

我想你不明白我在告訴你什麼。如果您在圖像視圖中繪製透明像素,則背後的任何內容都將顯示出來。 – 2009-12-23 00:53:46

0

你想要做的是將drawingColorAlpha設置爲0.這會創建一個相當於擦除的透明顏色。您可能還需要使用CGContextSetBlendMode()來設置混合模式。 kCGBlendModeNormal應該可以工作。

0

集混合模式如下: CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);

希望它能幫助你! :P