Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating raw image from Widget or Canvas

Tags:

flutter

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[])?

like image 448
ripple182 Avatar asked Jan 31 '17 12:01

ripple182


People also ask

How do you save a canvas on flutter?

Saving Image to GalleryThe FAB's onPressed() method calls the asynchronous _save() method to save the current state of canvas as image to gallery.


Video Answer


1 Answers

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.

like image 84
Andrei Tudor Diaconu Avatar answered Oct 08 '22 16:10

Andrei Tudor Diaconu