cropImage static method

Future<File?> cropImage({
  1. required String sourcePath,
  2. int? maxWidth,
  3. int? maxHeight,
  4. CropAspectRatio? aspectRatio,
  5. List<CropAspectRatioPreset> aspectRatioPresets = const [CropAspectRatioPreset.original, CropAspectRatioPreset.square, CropAspectRatioPreset.ratio3x2, CropAspectRatioPreset.ratio4x3, CropAspectRatioPreset.ratio16x9],
  6. CropStyle cropStyle = CropStyle.rectangle,
  7. ImageCompressFormat compressFormat = ImageCompressFormat.jpg,
  8. int compressQuality = 90,
  9. AndroidUiSettings? androidUiSettings,
  10. IOSUiSettings? iosUiSettings,
})

Launch cropper UI for an image.

parameters:

return:

A result file of the cropped image.

Note: The result file is saved in NSTemporaryDirectory on iOS and application Cache directory on Android, so it can be lost later, you are responsible for storing it somewhere permanent (if needed).

Implementation

static Future<File?> cropImage({
  required String sourcePath,
  int? maxWidth,
  int? maxHeight,
  CropAspectRatio? aspectRatio,
  List<CropAspectRatioPreset> aspectRatioPresets = const [
    CropAspectRatioPreset.original,
    CropAspectRatioPreset.square,
    CropAspectRatioPreset.ratio3x2,
    CropAspectRatioPreset.ratio4x3,
    CropAspectRatioPreset.ratio16x9
  ],
  CropStyle cropStyle = CropStyle.rectangle,
  ImageCompressFormat compressFormat = ImageCompressFormat.jpg,
  int compressQuality = 90,
  AndroidUiSettings? androidUiSettings,
  IOSUiSettings? iosUiSettings,
}) async {
  assert(await File(sourcePath).exists());
  assert(maxWidth == null || maxWidth > 0);
  assert(maxHeight == null || maxHeight > 0);
  assert(compressQuality >= 0 && compressQuality <= 100);

  final arguments = <String, dynamic>{
    'source_path': sourcePath,
    'max_width': maxWidth,
    'max_height': maxHeight,
    'ratio_x': aspectRatio?.ratioX,
    'ratio_y': aspectRatio?.ratioY,
    'aspect_ratio_presets':
        aspectRatioPresets.map<String>(aspectRatioPresetName).toList(),
    'crop_style': cropStyleName(cropStyle),
    'compress_format': compressFormatName(compressFormat),
    'compress_quality': compressQuality,
  }
    ..addAll(androidUiSettings?.toMap() ?? {})
    ..addAll(iosUiSettings?.toMap() ?? {});

  final String? resultPath =
      await _channel.invokeMethod('cropImage', arguments);
  return resultPath == null ? null : new File(resultPath);
}