CGContextShowTextAtPoint:上下文无效
我想调用一个方法,负责每 5 秒后在屏幕上绘制文本。这是我的代码
-(void) handleTimer: (NSTimer *)timer
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextTranslateCTM(context, 145.0, 240.0);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetCharacterSpacing(context, 1);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGContextSetRGBStrokeColor(context, 0.5,0.5,1,1);
CGContextShowTextAtPoint(context, 100, 100, "01", 2);
}
,但是 5 秒后,当调用此方法时,我收到此错误
CGContextShowTextAtPoint: invalid context
另一件事是如何显示更细的字体?
I want to call a method responsible for drawing text on screen after each 5 seconds. Here is my code
-(void) handleTimer: (NSTimer *)timer
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextTranslateCTM(context, 145.0, 240.0);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSelectFont(context, "Arial", 18, kCGEncodingMacRoman);
CGContextSetCharacterSpacing(context, 1);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGContextSetRGBStrokeColor(context, 0.5,0.5,1,1);
CGContextShowTextAtPoint(context, 100, 100, "01", 2);
}
But after 5 seconds when this method is called i am getting this error
CGContextShowTextAtPoint: invalid context
Another thing is how to show a thinner font?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,在绘制图形上下文之前清除图形上下文始终是一个好主意。
As I understand it, it's always a good idea to clear the graphics context before drawing to it.
在绘制之前,您必须拥有上下文。将绘制代码放入
drawRect
中会自动为您完成此操作(当您子类化 UIView 时,您会获得drawRect
)。您可以使用 UIGraphicsBeginImageContext(CGSize size) 创建您自己的上下文,尽管确保您的上下文保持有效可能会出现问题。
无论如何,目前对我来说肯定是有问题的。有谁对这方面有更多了解,想参与进来吗?
You've got to have a context before drawing into it. Putting draw code inside of
drawRect
does this automatically for you (you getdrawRect
when you subclass UIView).You can make your own context with
UIGraphicsBeginImageContext(CGSize size)
, although it can get problematic making sure that your context stays a valid one.Sure is problematic for me at the moment, anyway. Anyone knowing more about this want to weigh in?