capture method
Capture an image of the current state of corresponding BadCapture.
pixelRatio
: default to devicePixelRatio of MediaQuery.
Example:
// create a SnapshotKit instance
final cc = CaptureController();
// don't forget to attach the controller to a BadCapture
// e.g. BadCapture(controller: cc, child: Text('Hello'))
// capture the image and save it as png buffer
final image = cc.capture();
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
final Uint8List buffer = byteData!.buffer.asUint8List();
// do something with the buffer
Implementation
ui.Image capture([double? pixelRatio]) {
final ctx = _key.currentContext;
assert(ctx != null);
final boundary = ctx!.findRenderObject() as RenderRepaintBoundary;
pixelRatio ??= MediaQuery.of(ctx).devicePixelRatio;
return boundary.toImageSync(pixelRatio: pixelRatio);
}