CGContextShowTextAtPoint:上下文无效

发布于 2024-09-02 22:16:00 字数 764 浏览 2 评论 0原文

我想调用一个方法,负责每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

碍人泪离人颜 2024-09-09 22:16:00

据我了解,在绘制图形上下文之前清除图形上下文始终是一个好主意。

 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextClearRect(context, theRectInWhichYouWillBeDrawing);

As I understand it, it's always a good idea to clear the graphics context before drawing to it.

 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextClearRect(context, theRectInWhichYouWillBeDrawing);
独闯女儿国 2024-09-09 22:16:00

在绘制之前,您必须拥有上下文。将绘制代码放入 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 get drawRect 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文