繁体   English   中英

从UIImage清除UIView绘图

[英]clear UIView drawing from UIImage

好吧,快要吓死了...

制作绘图应用程序:主视图(按钮,广告等),然后是容器视图,占据了屏幕的一部分(带有自定义绘图类)当用户单击主视图中的按钮时,它将触发容器视图中的事件(可能是一种更好的方法,但是我找不到它)

当用户按下容器内的某个位置时,它将记录该点,添加一条线,然后更新draw:rect方法:

- (void)drawRect:(CGRect)rect
{
    [incImage drawInRect:rect];
    [path stroke];
}

(随着用户的绘制,将UIImage('incImage')添加到其上,并且将其绘制到视图上)

绘图代码:

- (void)drawBitmap
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
    [[UIColor colorWithRed:_brushR green:_brushG blue:_brushB alpha:_brushO] setStroke];
    [incImage drawAtPoint:CGPointZero];
    [path stroke];
    incImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

但是我无法清除视图! 我尝试将draw:Rect方法分为两部分(如果clear为true,否则为true),然后执行以下操作:

UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
rectpath = [UIBezierPath bezierPathWithRect:CGRectMake(0,0,incImage.size.width,incImage.size.height)];
[[UIColor whiteColor] setFill];
[rectpath fill];
[incImage drawAtPoint:CGPointZero];
incImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_clear=true;
[self setNeedsDisplay];

然后将draw:Rect更改为:

- (void)drawRect:(CGRect)rect
{
    if (clear) {
        [incImage drawInRect:rect];
        [rectpath fill];
    }
    else {
        [incImage drawInRect:rect];
        [path stroke];
    }
}

但这是行不通的(我还尝试过创建一个CGRect,然后用白色填充它并制作图像,然后尝试将该图像绘制到视图中,但是没有运气)。 我也检查过,问题不是主视图从容器视图进行通信和调用方法,我尝试使用该方法或draw方法的方式出了问题。

任何帮助将不胜感激,我的Mac的生活可能取决于它:)

创建如下函数:

-(void) clearDrawing {
    incImage = nil;
    [self setNeedsDisplay];
}

它将删除incImage,因此它将不再显示在-(void) drawRect 如果要再次显示某些内容,可以调用-(void)drawBitmap ,它将执行以下操作: incImage = UIGraphicsGetImageFromCurrentImageContext(); 并且incImage将通过一些绘图重新创建。

调用-drawRect: ,该视图将已经被擦除。 听起来您正在通过将它们添加到path来记录这些点。 当你想“清晰”的观点,您需要更新要存储的是真实所呈现的数据的对象-在你的情况,这可能意味着重置path通过调用-removeAllPoints或干脆扔掉旧的路径和启动新换一个新的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM