Hoo
Hoo

Reputation: 1840

save paint image without drop image quality

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

Answers (1)

Jim
Jim

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

Related Questions