toImage method

Future<Image> toImage()

Converts this image to a ui.Image. The caller is responsible for calling ui.Image.dispose() on the returned image.

Implementation

Future<ui.Image> toImage() async {
  final rtByteArray = _withThrowingErrorHandler((errorHandler) {
    return runtimecore.RT_Image_getBuffer(_handle, errorHandler);
  });

  // fromUint8List creates a copy
  final buffer = await ImmutableBuffer.fromUint8List(
    rtByteArray.data.asTypedList(rtByteArray.size),
  );

  final imageDescriptor = ui.ImageDescriptor.raw(
    buffer,
    width: width,
    height: height,
    pixelFormat: ui.PixelFormat.bgra8888,
  );
  final codec = await imageDescriptor.instantiateCodec();
  final frameInfo = await codec.getNextFrame();
  final uiImage = frameInfo.image.clone();
  frameInfo.image.dispose();

  buffer.dispose();

  return uiImage;
}