无论iphone中图像中出现的颜色如何,如何着色图像?

时间:2012-04-24 04:18:56

标签: iphone colors uiimage touch uibezierpath

我正在使用以下代码来触摸事件上的彩色图像。我有子类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];
}

1 个答案:

答案 0 :(得分:1)

尝试用

替换行[globalPath strokeWithBlendMode:kCGBlendModeOverlay alpha:1.0];
[globalPath strokeWithBlendMode:kCGBlendModeCopy alpha:1.0];
            ;

如果您想了解不同的混合模式的工作方式here(图2-1)。

Blend Modes