My end goal is to be able to share a dynamic image by either passing the raw image data or saving the image to disk for a native android/ios service to share. How would I convert a Widget
or Canvas
in flutter to raw image data (like byte[]
)?
Saving Image to GalleryThe FAB's onPressed() method calls the asynchronous _save() method to save the current state of canvas as image to gallery.
There is support for this now. You can either use RenderRepaintBoundry
or OffsetLayer
or Scene
. They all have a toImage
. Here is the link to RenderRepaintBoundry.toImage()
Future<void> _capturePng() async { RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject(); ui.Image image = await boundary.toImage(); ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png); Uint8List pngBytes = byteData.buffer.asUint8List(); print(pngBytes); }
This is copied from the docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With