【问题标题】:How can you change the color of a point in a UIView or a UIImageView?如何更改 UIView 或 UIImageView 中点的颜色?
【发布时间】:2013-01-31 14:20:25
【问题描述】:

我正在尝试在 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 个点)。

任何想法

【问题讨论】:

  • 你可以在图像视图之上放置一个自定义视图

标签: ios cocoa-touch uiview uiimageview uicolor


【解决方案1】:

使用

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

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

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

【讨论】:

    【解决方案2】:

    在您的drawRect 方法被调用之前,整个上下文被清除。您必须能够随时重新创建所有内容,而不仅仅是一个点。如果您要继承自己的另一个类,请不要忘记调用 super 实现,以便它可以添加其绘图贡献。

    【讨论】:

    • 1-“你必须能够重新创建所有内容”是什么意思
    • 当方法被调用时,你必须准备好所有的数据来创建你的视图保存的内容。例如,在您发布的代码中,您只绘制了一个矩形,但您应该将它们全部绘制(新的和旧的)。如果您的更新区域限制为一个矩形区域,您应该只更新该特定区域,但您仍然应该保留所有视图的数据(位置、颜色等)以供以后使用。
    • 所以请提供完整的想法,但是您能告诉我如何更新特定区域吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    相关资源
    最近更新 更多