Skip to main content
M
2mo ago

Is it possible to programmatically screenshot a Rive animation at a particular state in iOS?

What I want

I am showing a fullscreen Rive animation and when the user taps "Share", I want to export an image of the Rive animation at a particular state of the animation. I can't just programmatically take a screenshot of the entire screen (Example), as this would require the user to be viewing the particular state of the animation that I want to screenshot (They may tap share before this time).

What I tried

iOS SwiftUI has ImageRenderer, where you can provide a SwiftUI view and generate an image from it without the user actually seeing the view, but this does not work with RiveViewModel.view() (Image shows an error icon) and anyways Apple documentation states..

ImageRenderer output only includes views that SwiftUI renders, such as text, images, shapes, and composite views of these types. It does not render views provided by native platform frameworks (AppKit and UIKit) such as web views, media players, and some controls.

I also tried creating the RiveViewModel and showing the RiveViewModel.createRiveView() in a UIViewController, showing the UIViewController via UIViewControllerRepresentable, and then rendering a UIImage from the view (Example). This also doesn't work (Image is all white).

Question

What is the best way to accomplish my goal?

Thank you for any help!

3 replies
D
M
2mo ago

I can't just programmatically take a screenshot of the entire screen (Example), as this would require the user to be viewing the particular state of the animation that I want to screenshot (They may tap share before this time).

Am I understanding correctly that this solution would work as long as the share button would only appear when the state machine is in the right state? If so, you can use Rive Events to communicate with your Swift code.

M
2mo ago

Thank you for the response, Lance!

What you suggested is what I'm planning to do if there is not a "cleaner" way to do this. The downside of this approach is that we need to force the user into this state (Ex: If they tap to skip the animation, instead of immediately skipping, we first show the end of the animation so we can take the screenshot) and we need more communication between Rive and iOS code. All doable though.

Can you confirm then that there is no alternative/"cleaner" way?

Thank you again!

D
(edited) 2mo ago

We have an example of a view that takes a screenshot and writes out to a PNG; you might be able to use that as a starting point! It's available in our iOS repo here. You should also be able to do this "off-screen" (i.e when not added to a view hierarchy, so you might be able to just have a helper class that does this - create a rive view as the example, but don't add it as a subview anywhere).

You might have to figure out some way to bounce into UIKit from SwiftUI for the time being.