Как нарисовать ноль, используя cgcontext - PullRequest
1 голос
/ 23 декабря 2009

Я хочу нарисовать nil вместо цветов, потому что в конце я хочу, чтобы код стер часть раздела UIImageView, чтобы вы могли видеть, что стоит за UIImageView.

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

Ответы [ 4 ]

1 голос
/ 31 января 2010

Ответ Колина Гисласона неверен. Если вы установите 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)); 
0 голосов
/ 05 февраля 2011

Установите режим наложения следующим образом: CGContextSetBlendMode (UIGraphicsGetCurrentContext (), kCGBlendModeClear);

Надеюсь, это поможет тебе !! : P

0 голосов
/ 23 декабря 2009

То, что вы хотите сделать, это установить drawingColorAlpha в 0. Это создает прозрачный цвет, который эквивалентен стиранию. Возможно, вам также придется установить режим наложения, используя CGContextSetBlendMode(). kCGBlendModeNormal должно работать.

0 голосов
/ 23 декабря 2009

То, что вы хотите, это не ноль, а прозрачный цвет. Если ваш UIImageView не помечен как «непрозрачный» в IB, то он должен просвечивать в любых областях, где пиксели прозрачны.

...