png method
These pixels as a PNG file's bytes.
Implementation
Future<Uint8List> png() async {
// The engine takes raw pixels with their alpha multiplied in: worked
// out off the UI's isolate, as a big image takes a while.
final premultiplied = await compute(_premultiplied, rgba);
final buffer = await ui.ImmutableBuffer.fromUint8List(premultiplied);
ui.ImageDescriptor? descriptor;
ui.Codec? codec;
ui.Image? image;
try {
descriptor = ui.ImageDescriptor.raw(
buffer,
width: width,
height: height,
pixelFormat: ui.PixelFormat.rgba8888,
);
codec = await descriptor.instantiateCodec();
image = (await codec.getNextFrame()).image;
final data = await image.toByteData(format: ui.ImageByteFormat.png);
return data!.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
} finally {
// Let go however it went.
image?.dispose();
codec?.dispose();
descriptor?.dispose();
buffer.dispose();
}
}