Having UIView drawRect occur in a background threa

2019-01-19 04:34发布

I would like to have a UIView subclass that implements a method similar to setNeedsDisplay, except that redrawing (i.e., that would usually be called via drawRect:) will occur in a background thread sometime soonish, rather than at the end of the current update cycle.

It might be called setNeedsAsynchronousDisplay. Or the existing setNeedsDisplay could get hijacked and not cause redraw at the end of the cycle, or whatever, as long as it lets the redraw not happen on the main thread blocking screen updating an interaction until its completed.

Until the redraw occurs, the view can continue to use its current drawn representation.

Is something along these lines reasonably doable?

Thanks!

2条回答
何必那么认真
2楼-- · 2019-01-19 04:43

Yes it is possible. You will probably need to generate the content view as an image in the background and push the images into a nsdictionary or array.

So while your background is generating the images you can just show the image in drawrect function by rendering the image, providing the image has been generated.

A WWDC video that shows how to do it: WWDC 2012 session 211 - Building Concurrent User Interfaces on IOS. Here is the video description:

For a great user experience, it's essential to keep your application responsive while it renders complex UI elements and processes data. Learn how to use concurrency at the UIKit layer to perform drawing and other common operations without blocking user interaction.

查看更多
走好不送
3楼-- · 2019-01-19 04:47

No. View Drawing must occur in the foreground. Apple makes that very clear in their docs.

EDIT: You're right that you CAN do Core Graphics drawing in the background as long as it's not in a UIView object's drawing methods. You'd have to do the drawing in the background, then send a message to the main thread to update your view object once drawing is complete.

I would suggest not trying to override setNeedsDisplay. Instead, add your new setNeedsAsynchronousDisplay method. In that method, queue rendering code to an async queue using GCD calls. Once the rendering is complete, have the rendering code send a setNeedsDisplay message to self on the main thread.

Then in your subclass's drawRect method, check for pre rendered images and draw those into the view's context instead of the normal code.

A downside of this is that merely by implementing drawRect, you may slow down rendering because the system calls that instead of doing other, more efficient things to render your view's content.

查看更多
登录 后发表回答