簡體   English   中英

CGContext在線程中無法正常工作

[英]CGContext donesn't work correctly in thread

我正在嘗試使用Core Graphics從另一個線程在自定義UIView中進行無限繪圖。 無法獲得當前的CGContext所以我嘗試創建一個新的CGContext 但是我不知道如何將其應用於視圖。

這是我在自定義視圖中使用的代碼:

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM