image - 如何从使用 CustomPainter 绘制的画布中获取图像?
问题描述
在我的 Flutter 应用程序中,我使用CustomPainter来允许用户在屏幕上绘制他们的签名。我需要找到一种方法将其保存为图像。
当您能够PictureRecorder
按照之前的 StackOverflow 答案将对象传递到画布时, PictureRecorder可以很好地工作:
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,我怎样才能将记录器传递给画布?
解决方案
您不需要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);
}
推荐阅读
- postgresql - PostgreSQL alternative to LATERAL
- javascript - 选择下拉菜单中的新选择时,将 CSS 类恢复为原始类
- mysql - 比较两个表后,如何更新第一个表?
- c# - Reading and using appsettings.json in Program.cs?
- java - onBindViewHolder increase position onClickListener
- html - Why does opacity property carry over to an "irrelevant" element?
- angular - 如何解决 api 服务器对 angular/typescript 中的 http 请求的响应延迟?
- python - 使用 numpy 从对角线值创建数组堆栈
- javascript - How to prevent iframe of navigating and catch 404 error message?
- javascript - Eventbrite Custom Calendar Integration