在我的 Flutter 应用程序中,我使用了 CustomPainter允许用户在屏幕上绘制他们的签名。我需要找到一种方法将其保存为图像。
PictureRecorder当您能够通过 PictureRecorder
时效果很好根据 previous StackOverflow answers 将对象移到 Canvas 上:
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,我如何将记录器传递给 Canvas ?
最佳答案
您不需要通过 PictureRecorder
到 CustomPainter
中的 Canvas paint
方法。相反,您可以使用具有图片记录器的不同 Canvas 直接调用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);
}
关于image - 如何从用 CustomPainter 绘制的 Canvas 中获取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183607/