【问题标题】:how to convert image url to asset in flutter?如何在颤动中将图像网址转换为资产?
【发布时间】:2021-08-04 19:50:18
【问题描述】:

我使用 multi_image_picker 包从图库中获取图像。现在我想实现编辑页面,我需要将我的图像 url 列表转换为资产并将资产列表设置为 multi_image_picker。 这是我的多图像选择器:

List<Asset> resultList = <Asset>[];
String error = 'No Error Detected';

try {
  resultList = await MultiImagePicker.pickImages(
    maxImages: 300,
    enableCamera: true,
    selectedAssets: _imagesAssets,
    cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
    materialOptions: MaterialOptions(
      actionBarColor: "#abcdef",
      actionBarTitle: "صندوق شهدای موردک",
      allViewTitle: "تمام تصاویر",
      useDetailsView: false,
      selectCircleStrokeColor: "#000000",
    ),
  );
} on Exception catch (e) {
  error = e.toString();
}

// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;

setState(() {
  _imagesAssets = resultList;
});

}

以及用于显示所选图像的网格视图:

 return GridView.count(
  crossAxisCount: 2,
  shrinkWrap: true,
  physics: NeverScrollableScrollPhysics(),
  padding: EdgeInsets.all(10),
  children: List.generate(_imagesAssets.length, (index) {
    Asset asset = _imagesAssets[index];
    return AssetThumb(
      asset: asset,
      width: 100,
      height: 100,
    );
  }),
);

【问题讨论】:

  • 您在这里遇到的错误或问题是什么?有什么显示吗?什么都没有显示?显示错误?
  • 我有一个 url 列表,我想将其转换为资产列表以在 gridview 中显示图像

标签: flutter dart


【解决方案1】:

您不能将数据放入资产中,因为它是与您的应用捆绑和部署的文件,并且可以在运行时访问。您可以将其存储在数据库中或将其转换为 Uint8list。

【讨论】:

    【解决方案2】:

    我只需要这个功能(我用的是uuid pacakge):

    Future<Asset> fileToAsset(File image) async {
    String fileName = basename(image.path);
    var decodedImage = await decodeImageFromList(image.readAsBytesSync());
    return Asset(uuid.v4(), fileName, decodedImage.width, decodedImage.height);
    

    }

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2021-07-10
      • 2022-11-30
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多