0

我正在使用以下代码在触摸事件上为图像着色。我有子类 uiview 并使用以下代码来做到这一点。它正在工作,但图像不同部分的颜色不同。假设我用红色为图像着色并且有绿色,黄色或其他颜色出现在图像的不同部分。然后红色根据图像中已经存在的颜色具有不同的效果。而不是我希望在整个图像中填充相同的红色(或选择的任何颜色)。如何我做到了吗?

@synthesize selImage,isReset;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        arrBezierPaths = [[NSMutableArray alloc] init];
        globalPath = [[UIBezierPath alloc] init];
        myPath = [[UIBezierPath alloc] init];

        self.userInteractionEnabled = TRUE;

        myPath.lineWidth = 30;
        brushPattern = [UIColor redColor];

        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:myPath, @"Path", brushPattern, @"Color", nil];
        [arrBezierPaths addObject:dict];
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    for (int i=0; i<[arrBezierPaths count]; i++)
    {
        NSDictionary *dict = [arrBezierPaths objectAtIndex:i];
        UIColor *tempBrushpatter = [[UIColor alloc] init];
        tempBrushpatter = [dict valueForKey:@"Color"];
        globalPath = [dict valueForKey:@"Path"];

        [globalPath strokeWithBlendMode:kCGBlendModeOverlay alpha:1.0];
        [tempBrushpatter setStroke];
    }
}

#pragma mark - Touch Methods

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    [globalPath moveToPoint:[mytouch locationInView:self]];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    CGPoint pointTouched = [mytouch locationInView:self];

    for (int i=0; i<[arrBezierPaths count]; i++)
    {
        UIBezierPath *tempErasePath = [[UIBezierPath alloc] init];
        NSDictionary *dictTemp = [arrBezierPaths objectAtIndex:0];
        UIBezierPath *temppath = [dictTemp valueForKey:@"Path"];
        if ([temppath containsPoint:pointTouched])
        {
            //[temppath removeAllPoints];
        }
    }

    [globalPath addLineToPoint:[mytouch locationInView:self]];

    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}

-(void)changeColor:(UIColor *)color
{
    [arrRemovePaths removeAllObjects];
    UIBezierPath *temp = [[UIBezierPath alloc] init];

    temp.lineWidth = 30;
    UIColor *brushColor = color;
    NSLog(@"initWithFrame method called");
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:temp, @"Path", brushColor, @"Color", nil];
    [temp release];
    [arrBezierPaths addObject:dict];
    brushPattern = [color retain];
    [self setNeedsDisplay];
}

- (void)dealloc
{
    // [brushPattern release];
    [super dealloc];
}
4

1 回答 1

1

尝试[globalPath strokeWithBlendMode:kCGBlendModeOverlay alpha:1.0];

[globalPath strokeWithBlendMode:kCGBlendModeCopy alpha:1.0];
            ;

如果您想了解不同混合模式的工作原理,请看这里(图 2-1)。

混合模式

于 2012-04-24T04:37:51.033 回答