使用橡皮擦时为什么会出现黑色轮廓?

时间:2013-02-17 21:39:39

标签: iphone ios drawing uibezierpath erase

请参阅附图。我创建了一个应用程序,用户可以使用多个图像进行绘制,但是当我使用“清除颜色”以清除绘图时,它会显示擦除路径周围的黑色边框。我使用UIBezierPath进行绘图。这是UIBezierPath的问题还是可以通过其他任何方式解决?如果你想要代码片段,请告诉我!我会在这里发布。

enter image description here

修改:代码已添加!请忽略不相关的代码。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(isFirstTapp)
    {
        isFirstTapp=NO;
        [self.spooleteView removeHandView];
    }
    isdraw=YES;
    mouseSwapped=NO;
    UITouch *touch = [touches anyObject];
    lastPoint=[touch locationInView:self];

    //UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [currentPath moveToPoint:p];
    currentPath.flatness = 10.0;

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    UIBezierPath *myPath=[[UIBezierPath alloc]init];



    myPath.lineCapStyle=kCGLineJoinRound;
    [dict setObject:myPath forKey:@"Path"];
    colorTag= [[AppHelper userDefaultsForKey:@"ColorTag"]intValue];

    self.selectedColor = [brushColor objectAtIndex:colorTag];

    if(isErase)
    {
        myPath.lineWidth=30.0;
        self.selectedColor = [UIColor whiteColor];

    }
    else
    {
        if([mode isEqualToString:@"morsecode"])
            myPath.lineWidth=10.0;
        else
            myPath.lineWidth=5.0;
    }

    //[dict setObject:[brushColor objectAtIndex:colorTag] forKey:@"Colors"];
    [dict setObject:self.selectedColor forKey:@"Colors"];

    [myArr addObject:dict];
    currentPath=myPath;
    currentPath.flatness = 0.1;
    currentPath.lineJoinStyle = kCGLineJoinRound;
    currentDict=dict;
    [myPath moveToPoint:lastPoint];
    [myPath release];
    [dict release];    
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(!istapped)
    {
        if([mode isEqualToString:@"morsecode"])
        {
            mouseSwapped=YES;
            UITouch *mytouch=[[touches allObjects] objectAtIndex:0];

            currentPoint=[mytouch locationInView:self];

            [currentPath addLineToPoint:currentPoint];

            [self setNeedsDisplay];
        }
    }

//    UITouch *touch = [touches anyObject];
//    CGPoint p = [touch locationInView:self];
//    [currentPath addLineToPoint:p];
//    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(!istapped)
    {
        mouseSwapped=YES;

        UITouch *objTouch=[[touches allObjects] objectAtIndex:0];

        if (!mouseSwapped&&(objTouch.tapCount==1))
        {
            lastPoint=currentPoint;
        }
    }

    if(isErase)
    {
        if ([myArr count]>50)
        {
            [myArr removeAllObjects];
        }
    }

    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [currentPath addLineToPoint:p];
    [self drawBitmap]; // (3)
    [self setNeedsDisplay];
    [currentPath removeAllPoints]; //(4)
}

- (void)drawRect:(CGRect)rect
{
    [self.incrementalImage drawInRect:rect]; // (3)
    [currentPath stroke];

    //[self.incrementalImage drawInRect:rect];

    for (NSMutableDictionary *dictionary in myArr)
    {
        UIBezierPath *_path = [dictionary objectForKey:@"Path"];

        self.selectedColor = [dictionary objectForKey:@"Colors"];
        [self.selectedColor setStroke];
        [_path stroke];

        [_path strokeWithBlendMode:kCGBlendModeCopy alpha:1.0];
        _path.lineCapStyle = kCGLineJoinRound;

        if([mode isEqualToString:@"morsecode"])
        {
            if (!isErase)
            {
                const float p[2] = {1,15.9};
                [_path setLineDash:p count:2 phase:0.9];
            }

            else
            {
                self.selectedColor = [UIColor whiteColor];
            }
        }
    }
}

- (void)drawBitmap // (3)
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);

    [self.selectedColor setStroke];

    if (!self.incrementalImage) // first draw; paint background white by ...
    {
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
        [[UIColor clearColor] setFill];
        [rectpath fill]; // filling it with white
    }

    [self.incrementalImage drawAtPoint:CGPointZero];
    [currentPath stroke];
    self.incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

1 个答案:

答案 0 :(得分:0)

我找到了这个问题的原因。这是因为以下函数的第二行。

- (void)drawRect:(CGRect)rect
{
    [self.incrementalImage drawInRect:rect]; // (3)
    [currentPath stroke];

即,[currentPath stroke]; 我评论过,现在路径周围没有黑色边框。 我希望这会对其他人有所帮助。