0

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

在此处输入图像描述

编辑:已添加代码!请忽略不相关的代码。

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

1 回答 1

0

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

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

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

于 2013-02-17T23:40:08.843 回答