我正在尝试使用图片记录器将图像写到画布上...
drawImage需要dart:ui图像
我已经成功导入资产图像,但是网络图像出现以下错误:
[VERBOSE-2:codec.cc(97)]解码图像失败。数据是 无效,或者它是使用不支持的格式进行编码。
[VERBOSE-2:shell.cc(186)落镖错误:未处理的异常:异常: 手术失败 0 _rootHandleUncaughtError。 (dart:async / zone.dart:1112:29) 1个_microtaskLoop(dart:async / schedule_microtask.dart:41:21) 2 _startMicrotaskLoop(dart:async / schedule_microtask.dart:50:5)
代码试图到目前为止..
netImage.obtainKey(new ImageConfiguration()).then((val) {
var load = netImage.load(val);
load.addListener((listener, err) async {
ImageInfo imageInfo;
setState(() => imageInfo = listener);
imageInfo.image.toByteData().then((ss) {
Uint8List lst = new Uint8List.view(ss.buffer);
ui.instantiateImageCodec(lst).then((val) {
val.getNextFrame().then((vaal) {
imagetoDraw = vaal.image;
});
});
});
});
});
答案 0 :(得分:2)
我遇到了同样的问题。我通过以下代码段解决了该问题:
...
Completer<ImageInfo> completer = Completer();
Future<ui.Image> getImage(String path) async {
var img = new NetworkImage(path);
img.resolve(ImageConfiguration()).addListener(ImageStreamListener((ImageInfo info,bool _){
completer.complete(info);
}));
ImageInfo imageInfo = await completer.future;
return imageInfo.image;
}
....
答案 1 :(得分:0)
来自https://github.com/flutter/flutter/issues/23761
netImage.obtainKey(new ImageConfiguration()).then((val) {
var load = netImage.load(val);
load.addListener((listener, err) async {
setState(() => imageInfo = listener);
// imageInfo.image.toByteData().then((ss) {
//
// Uint8List lst = new Uint8List.view(ss.buffer);
//
//// ui.instantiateImageCodec(lst).then((val) {
//// val.getNextFrame().then((vaal) {
//// imagetoDraw = vaal.image;
//// });
//// });
// });
});
});
然后..
c.drawImage(imageInfo.image, offset3, paint);