majid
majid

Reputation: 33

FlutterImageCompress: AssertionError - "The jpeg format name must end with jpg or jpeg"

I'm encountering an issue with the flutter_image_compress package when trying to compress images captured using the DeepAR package in my Flutter app. The process works fine on Android, but on iOS, I get the following AssertionError:

'package:flutter_image_compress_platform_interface/src/validator.dart': Failed assertion: line 23 pos 9: '(name.endsWith('.jpg') || name.endsWith('.jpeg'))': The jpeg format name must end with jpg or jpeg.

Upon investigation, I found that the images captured on iOS using the DeepAR package have the .png extension by default. I tried changing the extension dynamically before passing it to flutter_image_compress, but this led to additional errors.

Future<List<File>> getFilesFromFilePaths(List<String> filePaths) async {
  List<File> compressedFiles = [];

  final String tempDir = await DeviceUtils.getTempFolder();
  await Future.forEach(filePaths, (String sourceFilePath) async {
    String filename = '_${basename(sourceFilePath)}';

    // Target file path where the compressed file will be saved
    String targetFilePath = '$tempDir/$filename';

    // Attempting to compress the image
    XFile? compressedFile = await FlutterImageCompress.compressAndGetFile(
      sourceFilePath,
      targetFilePath,
      quality: 70,
      autoCorrectionAngle: true,
    );
    print('***********************************');
    print(compressedFile);
    print('***********************************');
    if (compressedFile != null) {
      compressedFiles.add(File(compressedFile.path));
    }
  });

  return compressedFiles;
}

Upvotes: 0

Views: 43

Answers (1)

iveloff
iveloff

Reputation: 96

flutter_image_compress package supports png - mentioned here.

There is a special property inside FlutterImageCompress.compressAndGetFile(...) method called format. It takes CompressFormat enum, so you can provide CompressFormat.png.

XFile? compressedFile = await FlutterImageCompress.compressAndGetFile(
      ...,
      format: CompressFormat.png,
    );

No need to changing the extension dynamically before passing image inside that method.

Upvotes: 1

Related Questions