如何从网络映像中获取Flutter Uint8List? [英] How to get a Flutter Uint8List from a Network Image?

查看:1608
本文介绍了如何从网络映像中获取Flutter Uint8List?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将网络映像转换为文件,其中的第一部分是将其转换为Uint8List。这是我对其中的一张资产图片执行的操作...

I'm trying to convert a network image into a file and the first part of that is to convert it into a Uint8List. Here is how I'm doing this with 1 of my asset images...

      final ByteData bytes = await rootBundle.load('assests/logo');
      final Uint8List list = bytes.buffer.asUint8List();

      final tempDir = await getTemporaryDirectory();
      final file = await new File('${tempDir.path}/image.jpg').create();
      file.writeAsBytesSync(list);

如何使用 Image.network(imageUrl.com/image )

推荐答案

最简单的方法似乎是使用图像url和<$ c来获取http响应$ c> response.bodyBytes 将包含 Uint8List 中的数据。

The simplest way seeems to get the http response using the image url and response.bodyBytes would contain the data in Uint8List.

http.Response response = await http.get(
    'https://flutter.io/images/flutter-mark-square-100.png',
);   
response.bodyBytes //Uint8List

现在,您可以执行诸如转换为base64编码字符串的操作 base64.encode(response.bodyBytes);

Now you can do things like converting to base64 encoded string base64.encode(response.bodyBytes);

这篇关于如何从网络映像中获取Flutter Uint8List?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆