file_utils.dart 1014 B

12345678910111213141516171819202122232425262728293031
  1. import 'dart:io';
  2. import 'dart:typed_data';
  3. import 'dart:ui' as ui;
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/rendering.dart';
  6. import 'package:lszlgl/main.dart';
  7. import 'package:path_provider/path_provider.dart';
  8. class FileUtils {
  9. static Future<File> convertUint8ListToFile(Uint8List data, String fileName) async {
  10. final directory = await getApplicationCacheDirectory();
  11. final filePath = '${directory.path}/$fileName';
  12. final file = File(filePath);
  13. await file.writeAsBytes(data);
  14. return file;
  15. }
  16. /// 从Context中获取图片
  17. static Future<Uint8List?> getBitmapFromContext(
  18. BuildContext? context, {
  19. double pixelRatio = 3.0,
  20. }) async {
  21. if (context == null) return null;
  22. RenderRepaintBoundary render = context.findRenderObject() as RenderRepaintBoundary;
  23. ui.Image image = await render.toImage(pixelRatio: pixelRatio);
  24. ByteData? bytes = await image.toByteData(format: ui.ImageByteFormat.png);
  25. return bytes?.buffer.asUint8List();
  26. }
  27. }