Reputation: 1840
I allow user to paint on image. After save, the image quality will drop. How to prevent this happen?
Future<void> _save() async {
RenderRepaintBoundary boundary =
globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
Navigator.pop(context, pngBytes);
}
Full code here https://github.com/ptyagicodecamp/flutter_cookbook/blob/widgets/flutter_widgets/lib/canvas/painting.dart
Please help!
Upvotes: 1
Views: 742
Reputation: 7601
try to increase the pixelRatio, 2 works for me:
Future<void> _save() async {
RenderRepaintBoundary boundary =
globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 2.0);
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
Navigator.pop(context, pngBytes);
}
Upvotes: 2