flutter 图片压缩

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

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

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

_imgListUpload  = [];
ByteData byteData = await multiImages[i].getByteData(quality: 30);
List 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 图片压缩)