toImage method

Future<Image> toImage(
  1. Rect bounds,
  2. {double pixelRatio = 1.0}
)

Capture an image of the current state of this layer and its children.

The returned ui.Image has uncompressed raw RGBA bytes, will be offset by the top-left corner of bounds, and have dimensions equal to the size of bounds multiplied by pixelRatio.

The pixelRatio describes the scale between the logical pixels and the size of the output image. It is independent of the dart:ui.FlutterView.devicePixelRatio for the device, so specifying 1.0 (the default) will give you a 1:1 mapping between logical pixels and the output pixels in the image.

This API functions like toImageSync, except that it only returns after rasterization is complete.

See also:

Implementation

Future<ui.Image> toImage(Rect bounds, { double pixelRatio = 1.0 }) async {
  final ui.Scene scene = _createSceneForImage(bounds, pixelRatio: pixelRatio);

  try {
    // Size is rounded up to the next pixel to make sure we don't clip off
    // anything.
    return await scene.toImage(
      (pixelRatio * bounds.width).ceil(),
      (pixelRatio * bounds.height).ceil(),
    );
  } finally {
    scene.dispose();
  }
}