我正在尝试将网络映像转换为文件,并且该文件的第一部分是将其转换为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)
我需要将 NetworkImage 转换为 ui.Image。我尝试使用此问题给出的解决方案进行一些调整,但它不起作用。
有人可以帮助我吗?
Uint8List yourVar;
ui.Image image;
final DecoderCallback callback =
(Uint8List bytes, {int cacheWidth, int cacheHeight}) async {
yourVar = bytes.buffer.asUint8List();
var codec = await instantiateImageCodec(bytes,
targetWidth: cacheWidth, targetHeight: cacheHeight);
var frame = await codec.getNextFrame();
image = frame.image;
return image;
};
ImageProvider provider = NetworkImage(yourImageUrl);
provider.obtainKey(createLocalImageConfiguration(context)).then((key) {
provider.load(key, callback);
});
Run Code Online (Sandbox Code Playgroud)