Drawing in a background thread on iOS

前端 未结 2 888
既然无缘
既然无缘 2021-01-01 00:54

I have a view with some very complex drawing logic (it\'s a map view that draws from GIS data). Doing this drawing on the main thread locks up the UI and makes the app unres

相关标签:
2条回答
  • 2021-01-01 01:45

    Beyond iOS 4.0, drawing is thread safe. You might need to create a CGContext yourself, but there is no reason you cannot draw on a background thread.

    That said, most UIKit operations are not. If you need to do those, you can always prepare in a background thread, and then use performOnMainThread when needed. There's even waitUntilDone. That said, you should use NSOperation and NSOperationQueue, apparently.

    0 讨论(0)
  • 2021-01-01 01:49

    I have never came across such situation on iPhone but on Mac I had similar problem once.

    1. Use CGLayer to delegate drawing activity of offline contexts.
    2. Try add timer for current NSRunLoop and on timed interval execute your graphic commands. It should look something like this...

    ...

    kRenderFPS 25.0 //This is Maximum value
    
    renderTimer = [[NSTimer timerWithTimeInterval:(1.0 / (NSTimeInterval)kRenderFPS) target:viewobject selector:@selector(RenderUI:) userInfo:nil repeats:YES] retain];
    
    
    [[NSRunLoop currentRunLoop] addTimer:renderTimer forMode:NSDefaultRunLoopMode];
    
    
    [[NSRunLoop currentRunLoop] addTimer:renderTimer forMode:NSModalPanelRunLoopMode];
    
    
    [[NSRunLoop currentRunLoop] addTimer:renderTimer forMode:NSEventTrackingRunLoopMode];
    
    //In view class
    -(void)RenderUI:(id)param
    {
        [self setNeedsDisplayInRect:[self bounds]];
    }
    

    This should do the trick.

    Also try to sample your process and check who is consuming the CPU. This will make UI responsive and very fast.

    The performance problem you mentioned is can be due to something else. Try to cpu sample process. It will give you insight on who is actually taking CPU.

    0 讨论(0)
提交回复
热议问题