toImage method
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
window.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.
See also:
- RenderRepaintBoundary.toImage for a similar API at the render object level.
- dart:ui.Scene.toImage for more information about the image returned.
Implementation
Future<ui.Image> toImage(Rect bounds, {double pixelRatio = 1.0}) async {
assert(bounds != null);
assert(pixelRatio != null);
final ui.SceneBuilder builder = ui.SceneBuilder();
final Matrix4 transform = Matrix4.translationValues(
(-bounds.left - offset.dx) * pixelRatio,
(-bounds.top - offset.dy) * pixelRatio,
0.0,
);
transform.scale(pixelRatio, pixelRatio);
builder.pushTransform(transform.storage);
final ui.Scene scene = buildScene(builder);
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();
}
}