compressAssetImage static method

Future<Uint8List?> compressAssetImage(
  1. String assetName, {
  2. int minWidth = 1920,
  3. int minHeight = 1080,
  4. int quality = 95,
  5. int rotate = 0,
  6. bool autoCorrectionAngle = true,
  7. CompressFormat format = CompressFormat.jpeg,
  8. bool keepExif = false,
})

From asset to Uint8List

Implementation

static Future<Uint8List?> compressAssetImage(
  String assetName, {
  int minWidth = 1920,
  int minHeight = 1080,
  int quality = 95,
  int rotate = 0,
  bool autoCorrectionAngle = true,
  CompressFormat format = CompressFormat.jpeg,
  bool keepExif = false,
}) async {
  final support = await _validator.checkSupportPlatform(format);
  if (!support) {
    return null;
  }

  final img = AssetImage(assetName);
  final config = ImageConfiguration();

  AssetBundleImageKey key = await img.obtainKey(config);
  final ByteData data = await key.bundle.load(key.name);

  final uint8List = data.buffer.asUint8List();

  if (uint8List.isEmpty) {
    return null;
  }

  return compressWithList(
    uint8List,
    minHeight: minHeight,
    minWidth: minWidth,
    quality: quality,
    rotate: rotate,
    autoCorrectionAngle: autoCorrectionAngle,
    format: format,
    keepExif: keepExif,
  );
}