美文网首页Flutter
flutter 图片压缩

flutter 图片压缩

作者: 小轩言 | 来源:发表于2019-11-14 18:12 被阅读0次

    原以为很难的东西,东找找西找找,只有一个鲁班压缩,但都不怎么维护状态最终找到好的办法,通过dart自带的二进制压缩

    ByteData byteData = await Assets.getByteData(quality: 80);
    

    80就是压缩比例,默认为100
    Assets为获取到的图片类型
    上传的时候可以通过

    _imgListUpload  = [];
    ByteData byteData = await multiImages[i].getByteData(quality: 30);
    List<int> imageData = byteData.buffer.asUint8List();
     _imgListUpload.add(UploadFileInfo.fromBytes(imageData, name));
    

    multiImages就是存放Assets类型的图片数组,然后通过for循环遍历来批量添加图片

    var dio = Dio();
    List upload = [];
    upload.add(dio.post(
              "http://120.27.138.91:3009/api/containers/images/upload",
              options: new Options(
                  headers: {
                    'Authorization':GetApiToken.getToken().toString()
                  }
              ),
              data: FormData.from({
                "files": _imgListUpload != [] ? _imgListUpload : _imgListUpload = []
              }),
              onSendProgress: (received, total) {
                if (total != -1) {
                  setState(() {
                    value = (received / total * 100)/100;
                  });
                  print((received / total * 100).toStringAsFixed(0) + "%");
                }
              },
            ));
    

    用dio批量上传

    相关文章

      网友评论

        本文标题:flutter 图片压缩

        本文链接:https://www.haomeiwen.com/subject/yoxdictx.html