toImageSync method Null safety

Image toImageSync(
  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 toImage, except that rasterization begins eagerly on the raster thread and the image is returned before this is completed.

See also:

Implementation

ui.Image toImageSync(Rect bounds, { double pixelRatio = 1.0 }) {
  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 scene.toImageSync(
      (pixelRatio * bounds.width).ceil(),
      (pixelRatio * bounds.height).ceil(),
    );
  } finally {
    scene.dispose();
  }
}