fromImage static method
- Image image
Converts a dart:ui ui.Image to a StyleImage.
Reads ui.ImageByteFormat.rawRgba pixels, which are already premultiplied. This skips the PNG encode and decode steps.
The return type is StyleImage, not StyleImageRgba. This keeps room to switch to PNG later without a breaking change.
Implementation
static Future<StyleImage> fromImage(ui.Image image) async {
final byteData = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
if (byteData == null) {
throw StateError('Failed to read pixels from ui.Image.');
}
return StyleImage.rgba(
width: image.width,
height: image.height,
pixels: byteData.buffer.asUint8List(
byteData.offsetInBytes,
byteData.lengthInBytes,
),
);
}