我需要上传一定数量的照片并将这些照片发送到数据库。由于照片质量高,完成和上传每张照片需要相当长的时间。我不需要非常高质量的照片,所以我需要压缩照片。如果我使用 Flutter MultiImagePicker 类,最好的解决方案是什么?
List<Asset> pickedImagesList = await MultiImagePicker.pickImages(maxImages: 25, enableCamera: false);
最佳答案
您的包已经提供了一些选项来压缩 Asset
对象。
List<Asset> pickedImagesList = await MultiImagePicker.pickImages(maxImages: 25, enableCamera: false);
for (Asset asset in pickedImagesList) {
ByteData assetData = await asset.getThumbByteData(
width: // desired width,
height: // desired height,
quality: //desired quality,
);
// Send assetData to your database
}
编辑
我认为这可以保持您的纵横比:
double getAspectRatio(double originalSize, double desiredSize) => desiredSize / originalSize;
final aspectRatio = getAspectRatio(asset.originalWidth, imageDesiredWidth);
ByteData assetData = await asset.getThumbByteData(
width: (asset.originalWidth * aspectRatio).round(),
height: (asset.originalHeight * aspectRatio).round(),
quality: //desired quality,
);
关于image - 如何在 Flutter MultiImagePicker 中指定照片的质量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63069840/