1

I have created a sample app that saves a container as an image using flutter. This works totally fine with the android device and the saved image is of good quality. However, when I check this in the iPhone (emulator and physical device) the saved image is not in good quality.

Below is the code that I have used to save the container as an image and I just pass the global key of my container that needs to save.

import 'dart:math';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'dart:ui' as ui;
import 'dart:io' as Io;
import 'package:path_provider/path_provider.dart';
import 'package:gallery_saver/gallery_saver.dart';

class ImageExport {
  save(GlobalKey globalKey) async {
    Random random = new Random();
    int randomNumber = random.nextInt(1000);
    RenderRepaintBoundary boundary =
        globalKey.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage(pixelRatio: 5.0);
    final directory = (await getApplicationDocumentsDirectory()).path;
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    Io.File imgFile = new Io.File('$directory/test$randomNumber.jpg');
    imgFile.writeAsBytes(pngBytes);
    imgFile.writeAsBytesSync(pngBytes);
    GallerySaver.saveImage(imgFile.path, albumName: "test");
  }
}

Could some please help me resolve this issue?

Gihan
  • 53
  • 7
  • Did you manage to solve it? Please write a solution if you did. thanks. – mik May 24 '21 at 09:52
  • 2
    Hi, @mik I couldn't find a better solution. However, save quality is good when the image saves in PNG format. – Gihan May 30 '21 at 17:13

0 Answers0