如何从使用CustomPainter绘制的画布中获取图像?

时间:2019-05-17 09:35:17

标签: image canvas dart flutter custom-painting

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

当您能够按照PictureRecorderPictureRecorder对象传递到画布时,

previous StackOverflow answers可以很好地工作:

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

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

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

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

总而言之:

如何从CustomPainter生成图像?
如果答案是使用PictureRecorder,如何将录像机传递到画布上?

1 个答案:

答案 0 :(得分:0)

您无需通过PictureRecorder CustomPainter方法将paint传递到画布。相反,您可以直接使用具有图片记录器的其他画布直接调用绘画。例如:

Future<Image> getImage() async {
final PictureRecorder recorder = PictureRecorder();
myPainter.paint(Canvas(recorder), mySize);
final Picture picture = recorder.endRecording();

return await picture.toImage(width, height);
}