Capture view to video file

Hi,


My application requires a mode where the contents of a view are captured and recorded to a video file. The view contains an animation that I want to capture, as well as various controls that the user interacts with.


I know how to capture a UIImage from a view using drawViewHierarchyInRect and UIGraphicsGetImageFromCurrentImageContext. I also know how to take a series of these images, along with the timestamps from when they were captured, and render them to a video file (the code for this is complex, but it relies on AVAssetWriterInputPixelBufferAdaptor).


The problem I'm having is performance. I'd like to capture 30fps, but even on an iPhone 7, the best I can manage with this scheme is 20.


Writing the captured images to the video file isn't the issue, as this is being done on a low priority background thread. The problem is that doing the initial image capture takes a long time, around 44 milliseconds, and it has to be done on the main thread, which causes the whole app to perform poorly. Is there a faster way to do this?


I considered using the snapshotViewAfterScreenUpdates function of UIView to snapshot the views, figuring I could save them in memory and render them later on a low-priority thread. But this doesn't work. When I try to extract a UIImage from a view snapshot, all I get is a blank image.


Any thoughts as to how this can be done more efficiently?


Thanks,

Frank

Replies

Hi flarosa,


can you share your code how do you capture the view with animations? Mabye I can help you out but I need sample code for this.


Thanks,

Marcus

I appreciate the offer but the code is too complex to post in its entirety.


The problem is definitely capturing the screen images rather than rendering them to a video. I need a faster way to do this than drawViewHierarchyInRect and UIGraphicsGetImageFromCurrentImageContext. The only other thing I'm seeing is snapshotViewAfterScreenUpdates, which is documented as being faster than the other functions. The problem is that snapshotViewAfterScreenUpdates returns a UIView, but this UIView isn't identical to the original UIView. If I try to extract an image from it, I get a black rectangle.


So the question I'm asking boils down to: how do I extract an image from a UIView that was returned from snapshotViewAfterScreenUpdates?

Hi flarosa

Do you resolve this problem? I am doing the same thing as you, using drawViewHierarchyInRect and UIGraphicsGetImageFromCurrentImageContext to capture image from some views, and I can only get 12fps in iPad Pro.

No, I never found an alternative way to capture screen images. However, I did move my code to a background thread which helped with performance. Technically you aren't supposed to execute these functions off of the main thread, but doing so seems to work without issue, at least in my case.


Definitely do as little as possible in the thread that captures the images. Store them in memory and defer processing to another thread, or if possible don't process them at all until you're done with the whole recording session.


Frank