drawHierarchy speed

In iOS, I'm trying to do multiple (upwards of 10) screen snapshots (UIImages) per second to be combined into a video and am running into some challenges.

UIView's drawHierarchy seems to be to slow when run on the main thread, causing jitter with the UI experience. I've noticed that I can run it on a background thread, but only when passing a value of FALSE for the afterScreenUpdates parameter. This does cause the main thread warning to be written to the console (when the main thread checker is turned off), but does work. And I don't have to worry about blocking the main thread as it is on a background thread.

The question is, will running drawHierarchy on the background thread potentially cause memory corruption when traversing the entire tree of a UIWindow.

And if so, what is the advised most performant method of capturing iOS screenshots?

drawHierarchy Doc: https://developer.apple.com/documentation/uikit/uiview/1622589-drawhierarchy

TIA

Answered by Frameworks Engineer in 791593022

We cannot guarantee correctness or stability using UIView APIs from a secondary thread.

Capturing pixels like this in real time is generally not recommended in cases where performance is critical – APIs like ReplayKit use hardware blocks that can do this in a way that won't impact the user experience as much.

Why not use ReplayKit to record your video?

https://developer.apple.com/documentation/replaykit

Thanks for the quick replay. Apologies though, I should have mentioned that there is a desire to do this in such a way that the user's actions are not influenced. ReplayKit requires user approval.

Assume for the sake of argument that PII is not an issue, and that all content gets masked and anonymized. Really focussed on the technical challenge at the moment.

Accepted Answer

We cannot guarantee correctness or stability using UIView APIs from a secondary thread.

Capturing pixels like this in real time is generally not recommended in cases where performance is critical – APIs like ReplayKit use hardware blocks that can do this in a way that won't impact the user experience as much.

Great, I appreciate the response. Thank you!

drawHierarchy speed
 
 
Q