Ios 用手指画图
我正在写一个用户可以用手指画线的应用程序 这是代码:Ios 用手指画图,ios,objective-c,cocoa-touch,drawing,Ios,Objective C,Cocoa Touch,Drawing,我正在写一个用户可以用手指画线的应用程序 这是代码: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"BEGAN");//TEST OK UITouch* tap=[touches anyObject]; start_point=[tap locationInView:self]; } -(void)touchesMoved:(NSSet *)touches wit
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"BEGAN");//TEST OK
UITouch* tap=[touches anyObject];
start_point=[tap locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"MOVED");//TEST OK
UITouch* tap=[touches anyObject];
current_point=[tap locationInView:self];
[self DrawLine:start_point end:current_point];
start_point=current_point;
}
-(void)DrawLine: (CGPoint)start end:(CGPoint)end
{
context= UIGraphicsGetCurrentContext();
CGColorSpaceRef space_color= CGColorSpaceCreateDeviceRGB();
CGFloat component[]={1.0,0.0,0.0,1};
CGColorRef color = CGColorCreate(space_color, component);
//draw line
CGContextSetLineWidth(context, 1);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, start.x, start.y);
CGContextAddLineToPoint(context,end.x, end.y);
CGContextStrokePath(context);
}
我的问题是当我在屏幕上画线,但线是不可见的
我在应用程序的主要视图上绘制
context= UIGraphicsGetCurrentContext();
您正在从
drawRect:
方法外部调用UIGraphicsGetCurrentContext()
。因此它将返回nil
。因此,下面的函数试图利用一个实际为nil
的上下文,而该上下文显然无法工作,正如@Jorg所提到的,在drawRect:
方法之外没有当前上下文,因此UIGraphicsGetCurrentContext()
很可能返回nil
您可以使用CGLayerRef
进行屏幕外绘制,而在drawRect:
方法中,您可以在视图上绘制图层的内容
首先,您需要将层声明为类的成员,因此在@interface declareCGLayerRef\u offscreenLayer中代码>。您也可以为它创建一个属性,但是,在本例中我将直接使用它
在init方法中的某个地方:
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8, 4 * self.frame.size.width, colorspace, (uint32_t)kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorspace);
_offscreenLayer = CGLayerCreateWithContext(context, self.frame.size, NULL);
现在,让我们来处理图形:
-(void)DrawLine: (CGPoint)start end:(CGPoint)end
{
CGContextRef context = CGLayerGetContext(_offscreenLayer);
// ** draw your line using context defined above
[self setNeedsDisplay]; // or even better, use setNeedsDisplayInRect:, and compute the dirty rect using start and end points
}
-(void)drawRect:(CGRect)rect {
CGContextRef currentContext = UIGraphicsGetCurrentContext(); // this will work now, since we're in drawRect:
CGRect drawRect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
CGContextDrawLayerInRect(currentContext, drawRect, _offscreenLayer);
}
请注意,为了使代码正常工作,您可能需要进行一些小的更改,但应该让您了解如何实现它。在-(void)DrawLine:(CGPoint)start end:(CGPoint)end[自绘制线:start\u point arrivo:current\u point]中将“end”更改为“arrivo”
drawine:arrivo:
方法来自哪里?对不起,我是意大利人。[self DrawLine:start\u point arrivo:current\u point]是绘制线的方法。它在下面声明。否,它不在下面声明。仅当您向编译器教授意大利语时。该方法名为DrawLine:end:
,但您调用DrawLine:arrivo:
。这是不对的。如果它编译了,你要么给了我们错误的代码,要么没有给我们所有相关的代码;现在它画了线,但画得很慢,只画左下角的部分。问题是什么?很抱歉控制台出现错误,但我在iPhone上测试了这个应用程序,它没有在调试模式下启动。更好地解释一下我的不便:当我用手指画线时,线是看不见的,但当我去控制中心时(iOS 7上的菜单,我可以在其中启用Wi-Fi,脱机模式…)然后返回应用程序,该行可见。此外,该行仅在屏幕左下方的部分可见。感谢您的支持time@AlessioVinaccia我不知道,如果我没弄错的话,但我想你没有告诉iOS重新绘制视图,这样它就永远不会更新它(仅限从控制中心回来时)。在触摸结束时移动:withEvent:
呼叫[myView setNeedsDisplay]
对于您用来绘制线条的视图。这告诉iOS它需要重新绘制视图。这对他湿脚绝对有帮助。有一个问题:在CGLayerCreateWithContext中,drawContext应该是上下文。您认为“在屏幕外绘制”是什么意思屏幕上看不见的区域?@ AsisivoInCiCa在IOS中使用代码< > UIVIEW/COD>只能在drawRect:
内使用,否则在屏幕上不可见(例如,它可以保存在图像文件中,而不可见)。好的,我尝试了您的示例,但我遇到了以下控制台错误:“CGContextAddLineToPoint:无效的上下文0x0。这是一个严重错误。此应用程序或其使用的库正在使用无效的上下文,从而导致系统稳定性和可靠性的全面降低“对不起,我有一个问题要问你:你说只能在Drawrect方法中声明上下文,但在你的示例中,你也可以在Drawline方法中声明上下文。为什么?