从 Widget 或 Canvas 创建原始图像 [英] Creating raw image from Widget or Canvas

查看:22
本文介绍了从 Widget 或 Canvas 创建原始图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是能够通过传递原始图像数据或将图像保存到磁盘以供本机 android/ios 服务共享来共享动态图像.我如何将 flutter 中的 WidgetCanvas 转换为原始图像数据(如 byte[])?

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

推荐答案

现在有支持.您可以使用 RenderRepaintBoundryOffsetLayerScene.它们都有一个 toImage.这是RenderRepaintBoundry.toImage()

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);
  }

这是从文档中复制的.

这篇关于从 Widget 或 Canvas 创建原始图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆