【问题标题】:How to convert file back to Asset如何将文件转换回资产
【发布时间】:2021-01-18 16:02:03
【问题描述】:

我在pub.dev 上使用多文件选择器包,链接在https://pub.dev/packages/multi_image_picker 下方以及Yalantis 的图像裁剪器包 https://pub.dev/packages/image_cropper 让我的用户选择多张图像,然后随意裁剪它们。

我正在使用此代码将我的资产转换为文件并将其提供给裁剪器。它有效。

final temp = await Directory.systemTemp.createTemp();
final data = await finalList[index].getByteData();
File failo = await File('${temp.path}/img').writeAsBytes(
    data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));

print("The path is ${temp.path}");
File croppedFailo = await ImageCropper.cropImage(
  sourcePath: failo.path,
  androidUiSettings: AndroidUiSettings(toolbarTitle: "My App"),
);

棘手的一点是将它转换回资产,以便我可以用这个新裁剪的资产替换旧的 uncropped 资产。我通读了包的资产文档并尝试了这个,但它使我的应用程序崩溃

 Asset croppedPic = new Asset(
  croppedFailo.path,
  DateTime.now().millisecondsSinceEpoch.toString(),
  300,
  300,
);

finalList.replaceRange(index, index + 1, [croppedPic]);

编辑:当我说“资产”时,我指的不是我手动添加到应用程序的资产/图像文件夹中的图像。多图像选择器插件有一个称为资产的文件类型,它在其中返回图像。这就是我要将文件转换回的类型。

【问题讨论】:

  • “棘手的一点是将其转换回资产” - 抱歉,资产是只读的

标签: flutter flutter-packages cropper flutter-file


【解决方案1】:

没关系。我认为这样做太复杂了。因此,我只是重新格式化了我的整个代码以将图像作为文件而不是资产来处理。而且,它实际上让我的生活变得更简单,因为与资产相比,文件为您提供了更多的多功能性和更少的问题。

【讨论】:

    猜你喜欢
    • 2017-09-01
    • 2019-08-13
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多