IOS background drawing

I have a view with very complex drawing logic (this is a map view that is extracted from GIS data). Running this drawing on the main thread blocks the user interface and makes the application immune. I want to move a drawing to a background thread, such as NSOperation.

What is the best way to structure this?

I am currently drawing on a CGContext and then converting it to a CGImageRef, which I send to the view to blit on the main thread. Unfortunately, this greatly increases the amount of memory, and it seems that the GPU acceleration is no longer used, as it is a bit slower. Is there any way to draw directly in the view from the background thread? I know UIKit is not multi-threaded, but maybe there is some way to block the view while drawing?

+6
source share
2 answers

I have never encountered this situation on an iPhone, but on a Mac I had a similar problem.

  • Use CGLayer to delegate drawing activity to autonomous contexts.
  • Try to add a timer for the current NSRunLoop and execute graphical commands for the time interval. 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]]; } 

That should do the trick.

Also try trying your process and see who consumes the processor. This will make the UI responsive and very fast.

The performance issue you were talking about might be caused by something else. Try to process the processor sample. This will give you an idea of ​​who actually takes the processor.

+1
source

In addition to iOS 4.0, the drawing is thread safe . You may need to create a CGContext yourself, but there is no reason why you cannot draw a background thread.

However, most UIKit operations are not. If you need to do this, you can always prepare in the background thread, and then use performOnMainThread if necessary. There is even waitUntilDone . However, you should use NSOperation and NSOperationQueue , apparently.

+6
source

Source: https://habr.com/ru/post/898916/


All Articles