captureImage function

Future<Uint8List?> captureImage(
  1. GlobalKey<State<StatefulWidget>> globalKey
)

Implementation

Future<Uint8List?> captureImage(GlobalKey globalKey) async {
  try {
    RenderRepaintBoundary boundary =
        globalKey.currentContext?.findRenderObject() as RenderRepaintBoundary;
    double dpr = ui.window.devicePixelRatio; // 获取当前设备的像素比
    ui.Image image = await boundary.toImage(pixelRatio: dpr);
    print(image);
    ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    return byteData!.buffer.asUint8List();
  } catch (e) {
    print(e);
    return null;
  }
}