1

我正在尝试在 UIView 中构建自己的“Flood Fill”实现。一切进展顺利,但我不知道如何更改 UIImageView 或 UIView 中特定点/像素的颜色。

如何更改 UIView 中点或像素的颜色?

编辑:

这是我的场景:

1-触摸事件:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint loc = [touch locationInView:_image];
    UIColor *mycol=[self getPixelColorAtLocation:loc];
    if (mycondition) {

        _mylayer->point_=loc;
        _mylayer->color_=mycol;
        [_mylayer setNeedsDisplay];

    }
}

在我的自定义上绘制矩形UIView

- (void)drawRect:(CGRect)rect {
    if (point_.x>0) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(context, color_.CGColor);
        CGContextFillRect(context, CGRectMake(point_.x,point_.y, 1.0, 1.0));
    }
}

我现在的观点是“每次我调用[_mylayer setNeedsDisplay];放置的点(新颜色)时,都会删除一个新点(只有 1 个点)。

任何想法

4

2 回答 2

0

Before your drawRect method is called the entire context is cleared. You have to be able to recreate all your content at any time, not just one point. If you're subclassing another class of your own, don't forget to call the super implementation so it can add its drawing contribution.

于 2013-02-18T12:38:00.307 回答
0

利用

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

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

改变点/像素颜色的方法。

于 2013-02-18T11:47:41.593 回答