Cha*_* Jr 6 byte image file dart flutter
我正在尝试将网络映像转换为文件,并且该文件的第一部分是将其转换为Uint8List。这是我对其中的一张资产图片执行的操作...
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);
Run Code Online (Sandbox Code Playgroud)
我该怎么做 Image.network(imageUrl.com/image)
lor*_*vcs 16
最简单的方法似乎是使用图像 url 获取 http 响应,response.bodyBytes
并将数据包含在Uint8List
.
http.Response response = await http.get(
'https://flutter.io/images/flutter-mark-square-100.png',
);
response.bodyBytes //Uint8List
Run Code Online (Sandbox Code Playgroud)
现在您可以执行诸如转换为 base64 编码的字符串之类的操作base64.encode(response.bodyBytes);
更新:使用较新版本的 http,您需要添加 Uri.parse()
例如。
http.Response response = await http.get(
Uri.parse('https://flutter.io/images/flutter-mark-square-100.png'),
);
Run Code Online (Sandbox Code Playgroud)
Ujj*_*ada 11
我想出了一个不同的解决方案。希望它能帮助某人。
import 'dart:typed_data';
import 'package:flutter/services.dart';
Uint8List bytes = (await NetworkAssetBundle(Uri.parse(imageUrl))
.load(imageUrl))
.buffer
.asUint8List();
Run Code Online (Sandbox Code Playgroud)
void initState() {
super.initState();
var sunImage = new NetworkImage(
"https://resources.ninghao.org/images/childhood-in-a-picture.jpg");
sunImage.obtainKey(new ImageConfiguration()).then((val) {
var load = sunImage.load(val);
load.addListener((listener, err) async {
setState(() => image = listener);
});
});
}
Run Code Online (Sandbox Code Playgroud)
另见https://github.com/flutter/flutter/issues/23761#issuecomment-434606683
然后你可以使用 image.toByteData().buffer.asUInt8List()
另见https://docs.flutter.io/flutter/dart-ui/Image/toByteData.html
归档时间: |
|
查看次数: |
5261 次 |
最近记录: |