I have a class that takes in an image, and returns an image compressed to 50kb. However, an error is occurring in the while loop. It seems to not be reducing image quality past the the first iteration.
class CompressToMin {
static Future<File> compressToMin(File image) async {
int count = 0;
print('Size before compression: ${image.lengthSync()}');
int len = image.lengthSync();
File? temp = image;
while (len > 50000) {
final String fileType = image.path.substring(image.path.lastIndexOf('.'));
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path + count.toString() + '.' + fileType;
temp = await compress(temp!, tempPath);
count++;
len = temp!.lengthSync();
('length after compression: ${temp.lengthSync()}');
}
return temp!;
}
}
Future<File?> compress(File file, String targetPath) async {
final result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path,
targetPath,
quality: 5,
);
return result;
}
Am i doing something wrong? Thank you!