【发布时间】:2014-01-15 15:43:35
【问题描述】:
好吧,快吓坏了……
制作绘图应用程序: 主视图(按钮、广告等) 然后是占据屏幕一部分的容器视图(带有自定义绘图类) 当用户单击主视图中的按钮时,它会在容器视图中触发一个事件(可能是更好的方法,但我找不到)
当用户按下容器内的某个位置时,它会记录该点,添加一条线,然后更新 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 为真,如果不是)并这样做:
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 然后用白色填充它并制作图像并尝试将该图像绘制到视图但没有运气)。我也检查过,问题不是主视图从容器视图通信和调用方法,我试图解决这个问题的方式或我的绘制方法有问题。
任何帮助将不胜感激,我的 Mac 的生命可能取决于它:)
【问题讨论】:
-
您是否在任何地方清除矩形?我没有看到它,但不确定我们是否看到了所有代码。如果没有,请将其添加到您的 drawRect:CGContextClearRect(ctx, rect);
-
我确实尝试在我的 draw:rect 方法中这样做: - (void)drawRect:(CGRect)rect { if (!_clr) { [incImage drawInRect:rect]; [路径行程]; } if (_clr) { CGContextClearRect(UIGraphicsGetCurrentContext(), rect); incImage = UIGraphicsGetImageFromCurrentImageContext(); [incImage drawInRect:rect]; } } 还是什么都没有
标签: ios objective-c uiview uiimage