Converting a byte array to image in Flutter?

五迷三道 提交于 2021-02-19 02:53:29

问题


I want to get a image from my REST API service however haven't found any documentation on how to decode the response body that would be a byte array to an image in Flutter? Anyone with some useful resources, please help...


回答1:


Use this for your image widget: Image.memory(bytes) . You can find more documentation here: https://docs.flutter.io/flutter/widgets/Image/Image.memory.html




回答2:


Since top rated answer use flutter/widget.dart, this use dart:ui only

Future<Image> tinypng() async {
  final bytes = Uint8List.fromList([
    137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0,
    1, 0, 0, 0, 1, 8, 6, 0, 0, 0, 31, 21, 196, 137, 0, 0, 0, 10, 73, 68, 65,
    84, 120, 156, 99, 0, 1, 0, 0, 5, 0, 1, 13, 10, 45, 180, 0, 0, 0, 0, 73,
    69, 78, 68, 174, 66, 96, 130 // prevent dartfmt
  ]);

  // copy from decodeImageFromList of package:flutter/painting.dart
  final codec = await instantiateImageCodec(bytes);
  final frameInfo = await codec.getNextFrame();
  return frameInfo.image;
}


来源:https://stackoverflow.com/questions/55314081/converting-a-byte-array-to-image-in-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!