Quick start
Wrap the content in a ViewShotView and call capture() on its ref:
import { useRef } from 'react'
import { Button, Text, View } from 'nitro-ui-renderer'
import { ViewShotView, type ViewShotViewRef } from 'nitro-view-shot'
export function Card() {
const shot = useRef<ViewShotViewRef>(null)
const save = async () => {
const image = await shot.current?.capture()
const png = await image?.toEncodedImageDataAsync('png')
// png.buffer holds the encoded bytes
}
return (
<View>
<ViewShotView ref={shot} style={{ background: 'white', cornerRadius: 16 }}>
<Text>Captured content</Text>
</ViewShotView>
<Button label="Capture" onPress={save} />
</View>
)
}
ViewShotViewrenders a NitroUIView; it takes everyViewProps.shot.current.capture()resolves to anitro-ui-rendererImageof the view and its children.toEncodedImageDataAsync('png')encodes it; the bytes are inpng.buffer.
Capture a view you already have
To capture any mounted NitroUI View, pass its ref value to ViewShot.capture:
import { ViewShot } from 'nitro-view-shot'
const image = await ViewShot.capture(view) // view: ViewRef from useRef<ViewRef>(null).current
Show the result
An Image can go straight into NitroUI's Image component. From the example app:
<Image
image={shot}
resizeMode="contain"
layout={{ width: 300, aspectRatio: shot.width / shot.height }}
/>
Next: what a capture includes.