Open carrasc0 opened 2 years ago
As there is initAspectRatio
as it's available only in Android. I did some workaround by specify rectX
, rectY
, rectWidth
, rectHeight
into IOSUiSettings. To determine these data, I did some calculation using
Future<Map<String, double>> getIOSInitialRect(XFile image) async {
try {
Map<String, double> result = {'rectX': 0, 'rectY': 0, 'rectWidth': 0, 'rectHeight': 0};
final imageBytes = await image.readAsBytes();
final decodedImage = await decodeImageFromList(imageBytes);
int height = decodedImage.height;
int width = decodedImage.width;
bool isLandscape = width > height;
if (isLandscape) {
result['rectWidth'] = height.toDouble();
result['rectHeight'] = height.toDouble();
result['rectY'] = 0;
result['rectX'] = width - height.toDouble();
} else {
result['rectWidth'] = width.toDouble();
result['rectHeight'] = width.toDouble();
result['rectX'] = 0;
result['rectY'] = height - width.toDouble();
}
return result;
} catch (exeption, stackTrace) {
print('cannot get ios initial crop');
}
return null;
}
And my crop image function looks like this (for me, I only accept square image)
Future<String> _cropImage(XFile image) async {
Map<String, double> iOSRect = await getIOSInitialRect(image);
File croppedFile = await ImageCropper.cropImage(
sourcePath: image.path,
aspectRatioPresets: Platform.isAndroid
? [
CropAspectRatioPreset.square,
]
: [
CropAspectRatioPreset.square,
],
androidUiSettings: AndroidUiSettings(
toolbarTitle: 'Cropper',
toolbarColor: Style.primaryColor,
toolbarWidgetColor: Colors.white,
initAspectRatio: CropAspectRatioPreset.square,
lockAspectRatio: true),
iosUiSettings: iOSRect != null
? IOSUiSettings(
title: 'Cropper',
rectX: iOSRect['rectX'],
rectY: iOSRect['rectY'],
rectWidth: iOSRect['rectWidth'],
rectHeight: iOSRect['rectHeight'])
: IOSUiSettings(title: 'Cropper'),
);
if (croppedFile != null) {
return croppedFile.path;
}
return null;
}
It is possible to add init aspect ratio for iOS?
Yes. Use the aspectRatio property like:
ImageCropper().cropImage(
aspectRatio: const CropAspectRatio(ratioX: 16, ratioY: 9),
androidUiSettings: AndroidUiSettings(...),
iosUiSettings: IOSUiSettings(...),
);
It is possible to add init aspect ratio for iOS?