encodeToFilePath static method

Future<String> encodeToFilePath({
  1. required Image image,
  2. required String filePath,
  3. MPFlutterImageByteFormat format = MPFlutterImageByteFormat.png,
  4. double compressQuality = 0.92,
})

直接将 Image 保存成小程序文件,然后你可以使用 WX API 上传到服务器,或使用 WX API 保存到用户相册。

Implementation

static Future<String> encodeToFilePath({
  required ui.Image image,
  required String filePath,
  MPFlutterImageByteFormat format = MPFlutterImageByteFormat.png,
  double compressQuality = 0.92,
}) async {
  final rawRgba = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
  if (rawRgba == null) {
    throw "no raw rgba data";
  }
  final completer = Completer<String>();
  (mpjs.context["encodeImageToFilePath"] as mpjs.JSFunction).call([
    rawRgba.buffer.asUint8List(),
    image.width,
    image.height,
    "image/${format.name}",
    compressQuality,
    filePath,
    (String filePath) {
      if (filePath.isEmpty) {
        throw "save file failed";
      }
      completer.complete(filePath);
    }
  ]);
  return completer.future;
}