ios - CGContext 在线程中无法正常工作

标签 ios thread-safety core-graphics drawrect cgcontext

我正在尝试使用 Core Graphics 从另一个线程在自定义 UIView 中进行无限绘图。无法获取当前的 CGContext,因此我尝试创建一个新的。但我不知道如何将其应用到 View 中。

这是我在自定义 View 中使用的代码:

CG_INLINE CGContextRef CGContextCreate(CGSize size)
{
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(nil, size.width, size.height, 8, size.width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(space);

    return ctx;
}
- (void)drawRect:(CGRect)rect 
{
    CGContextRef context = CGContextCreate(self.bounds.size);
    if(context)
    {
        [self.layer renderInContext:context];
        [self drawSymbolsInContext:context];
        CGContextRelease(context);
    }
}
- (void)drawSymbolsInContext:(CGContextRef)context
{
    for (int x = 0; x<[cellsArray count];x++)
    {
        for(int y=0; y<[[cellsArray objectAtIndex:x] count]; y++)
        {
            NSLog(@"lol %d", y);

            float white[] = {1.0, 1.0, 1.0, 1.0};
            CGContextSetFillColor(context, white);
            CGContextFillRect(context, [self bounds]);

            CGContextSetTextDrawingMode(context, kCGTextStroke);
            CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
            CGContextSelectFont(context, "Times", 12.0, kCGEncodingMacRoman);
                                CGAffineTransform xform = CGAffineTransformMake(
                                                                                1.0,  0.0,
                                                                                0.0, -1.0,
                                                                                0.0,  0.0);
            CGContextSetTextMatrix(context, xform);
            CGContextShowTextAtPoint(context, 100.0, 100.0 + 15*y, "test", strlen("test"));
        }
    }
}

这是我用来运行 thead 的代码(在 ViewController 中):

- (void)viewDidLoad
{
    [super viewDidLoad];
    [NSThread detachNewThreadSelector:@selector(SomeAction:) toTarget:self withObject:self.view];
}

- (void) SomeAction:(id)anObject
{
    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
    DrawView *drawView = (DrawView *)anObject;

    while (1)
    {
        COLOR col;
        col.blue = 100;
        col.red = 100;
        col.green = 200;

        int cellX = 0;
        int cellY = [[rowsArray objectAtIndex:0] count];

        CELL cel;
        cel.x = cellX;
        cel.x = cellY;

        SymbolAlive *s = [[SymbolAlive alloc] initWithColor:col andFrame:cel];

        [[rowsArray objectAtIndex:0] addObject:s];
        [drawView drawRect:drawView.bounds];

        [NSThread sleepForTimeInterval: 0.1];
    }
    [NSThread exit];
    [autoreleasepool release];
}

循环正确运行。但屏幕上没有显示任何内容。

有什么想法吗?

最佳答案

我猜你必须在主线程上进行绘图。而不是

    [drawView drawRect:drawView.bounds];

这样做

   [self performSelectorOnMainThread:@selector(doDraw:) withObject:nil waitUntilDone:NO];

然后创建一个绘图函数,例如

    - (void)doDraw:(id)notused
    {
        [drawView drawRect:drawView.bounds];
    }

这将允许您在后台进行处理,但在主线程上进行绘图。

此外,在这种情况下您应该使用标准图形上下文。

还有一点,您可能还可以将所有绘图移到上下文中到后台线程中,然后在主线程上调用 renderInContext 。

关于ios - CGContext 在线程中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12493279/

相关文章:

ios - 表格单元格未正确调整大小

ios - 不需要更新 Realm 中的对象

ios - 移植到 iOS 7 的应用程序现在请求麦克风访问

ios - 在drawRect()方法中绘制CoreGraphics线时遇到问题

swift - macOS 画一个矩形,里面有两个洞

ios - 标签栏项目色调颜色

iOS-[UIImage initWithCGImage :scale:orientation:] crashes on a background thread

c++ - 单例线程安全类

Java选择性同步

ios - map 的自定义注释 View