在我的Flutter应用程序中,我使用CustomPainter来允许用户在屏幕上绘制其签名。我需要找到一种方法将其另存为图像。
当您能够按照PictureRecorder将PictureRecorder
对象传递到画布时,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,如何将录像机传递到画布上?
答案 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);
}