【flutter压缩Uint8List图片大小】

如果没有引入flutter_cache_manager库需要引入
例如flutter_cache_manager: 3.3.0

import 'package:image/image.dart' as image;
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

_compressImage(String mirrorImage){
   
	Uint8List imageBytes;
    if(mirrorImage.startsWith('http')){
   
      final File file = await DefaultCacheManager().getSingleFile(mirrorImage);
      final image.Image thumbnail = image.decodeImage(file.readAsBytesSync());
      imageBytes = thumbnail.getBytes();
    }else{
   
      imageBytes = await File(mirrorImage).readAsBytes();
    }
 	print("图片初始大小:${
     imageBytes .length/ 1024 /1024}MB");
    if (imageBytes.length > 1024 * 1024 * 0.5) {
   
      int _quality = 90;
      while(imageBytes.length > 1024 * 1024 * 0.5 && _quality>0){
   
        imageBytes = Uint8List.fromList(image.encodeJpg(thumbnail,quality: _quality));
        _quality -= 2;
      }
      print("图片压缩后大小:${
     thumbData.length} ${
     thumbData.length/ 1024 / 1024}MB");
    }
    }

相关推荐

  1. flutter压缩Uint8List图片大小

    2023-12-09 12:10:02       63 阅读
  2. uni-app H5上传图片压缩体积大小

    2023-12-09 12:10:02       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-09 12:10:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-09 12:10:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-09 12:10:02       87 阅读
  4. Python语言-面向对象

    2023-12-09 12:10:02       96 阅读

热门阅读

  1. AIGC: 关于ChatGPT中实现一个聊天机器人

    2023-12-09 12:10:02       58 阅读
  2. .ros空间的清理

    2023-12-09 12:10:02       53 阅读
  3. centos7.9安装k8s v1.28.4

    2023-12-09 12:10:02       42 阅读
  4. ubuntu20.04设置开机自启动jar(依赖其他服务)

    2023-12-09 12:10:02       70 阅读
  5. SQL Server事务(Transaction)

    2023-12-09 12:10:02       55 阅读
  6. 【Docker】进阶之路:(十)Docker日志管理

    2023-12-09 12:10:02       42 阅读
  7. ffmpeg与opencv-python处理视频

    2023-12-09 12:10:02       57 阅读
  8. 鸿蒙(HarmonyOS)应用开发——http的使用

    2023-12-09 12:10:02       65 阅读
  9. LeetCode刷题笔记之链表

    2023-12-09 12:10:02       46 阅读
  10. 人大金仓(kingbase)数据库常用sql命令

    2023-12-09 12:10:02       47 阅读