在谷歌地图上显示自定义小部件谷歌地图颤振 https://pub.dev/packages/google_maps_flutter我使用一个非常方便的功能来翻译任何Widget
(包括那些包含图像的)到Image
,然后我将其翻译成Uint8List
我需要。它在 iOS 和 Android 上运行良好。
Future createImageFromWidget( Widget widget){
final RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();
final RenderView renderView = RenderView(
child: RenderPositionedBox(alignment: Alignment.center, child: repaintBoundary),
configuration: ViewConfiguration(size: const Size.square(300.0), devicePixelRatio: ui.window.devicePixelRatio),
window: null,
);
final PipelineOwner pipelineOwner = PipelineOwner()..rootNode = renderView;
renderView.prepareInitialFrame();
final BuildOwner buildOwner = BuildOwner(focusManager: FocusManager());
final RenderObjectToWidgetElement<RenderBox> rootElement = RenderObjectToWidgetAdapter<RenderBox>(
container: repaintBoundary,
child: Directionality(
textDirection: TextDirection.ltr,
child: IntrinsicHeight(child: IntrinsicWidth(child: widget)),
),
).attachToRenderTree(buildOwner);
buildOwner..buildScope(rootElement)..finalizeTree();
pipelineOwner..flushLayout()..flushCompositingBits()..flushPaint();
return repaintBoundary.toImage(pixelRatio: ui.window.devicePixelRatio)
.then((image) => image.toByteData(format: ui.ImageByteFormat.png))
.then((byteData) => byteData.buffer.asUint8List());
}
但不幸的是,当我尝试在网络上使用此功能时,出现以下错误:
不支持的操作:Web 上不支持 toImage
现在 Flutter 已经支持Image.toByteData
and Picture.toImage
#20750 https://github.com/flutter/engine/pull/20750,但我不知道如何在我的案例中使用它。
实际上问题是 - 如何重新设计这个功能,使其也能在 Web 上运行?
谢谢,我很高兴有任何想法
toImage() 如果你把--dart-define=FLUTTER_WEB_USE_SKIA
on flutter build web
论据。
该方法在没有此参数的情况下在本地运行。
我看到这个解决方案后发现crop https://pub.dev/packages/crop包文档,它也使用了 toImage()
EDIT
通过 iPhone 访问应用程序时,我发现了一些问题,从小部件生成图像可以工作,但项目中的一些动画停止工作。
我找到了这个question https://stackoverflow.com/questions/64583461/how-to-use-skia-canvaskit-in-flutter-web我将仅使用参数进行一些测试--dart-define=FLUTTER_WEB_AUTO_DETECT=true
查看 Flutter Web 是否可以在桌面、Android 和 iOS 上正常工作。
EDIT 2
经过在 MacO 上的一些测试,toImage() 方法也可以正常工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)