【问题标题】:How to get an image from a canvas drawn with CustomPainter?如何从使用 CustomPainter 绘制的画布中获取图像?
【发布时间】:2019-10-04 14:53:24
【问题描述】:

在我的 Flutter 应用程序中,我使用 CustomPainter 来允许用户在屏幕上绘制他们的签名。我需要找到一种方法将其保存为图像。

PictureRecorder 在您能够按照previous StackOverflow answersPictureRecorder 对象传递到画布时效果很好:

final recorder = new PictureRecorder();
Canvas(recorder).drawSomething;
final picture = recorder.endRecording();

但是,当使用 CustomPainter 时,画布是 Paint() 函数的参数。

class myPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    drawToCanvas(canvas);

  @override
  bool shouldRepaint(CustomPainter old) {
    return false; 
}

总结一下:

如何从 CustomPainter 生成图像?
如果答案是使用 PictureRecorder,我怎样才能将记录器传递给画布?

【问题讨论】:

    标签: image canvas dart flutter custom-painting


    【解决方案1】:

    您不需要在CustomPainter paint 方法中将PictureRecorder 传递给画布。相反,您可以使用具有图片记录器的不同画布直接调用绘画。例如:

    Future<Image> getImage() async {
    final PictureRecorder recorder = PictureRecorder();
    myPainter.paint(Canvas(recorder), mySize);
    final Picture picture = recorder.endRecording();
    
    return await picture.toImage(width, height);
    }
    
    

    【讨论】:

    • 嗨,我的朋友,我在尝试执行您的答案时出错。 myPainter 是一个类,那么你如何管理它?
    猜你喜欢
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多