I am using the flutter package extended_image to edit images.
The load of the image and editing work perfectly (visually) on screen
STATE VARAIBLES:
final GlobalKey<ExtendedImageEditorState> editorKey = GlobalKey<ExtendedImageEditorState>();
late ExtendedImage img;
LOAD:
img = ExtendedImage.file(
File(widget.filePath),
fit: BoxFit.contain,
mode: ExtendedImageMode.editor,
extendedImageEditorKey: editorKey,
cacheRawData: true,
initEditorConfigHandler: (state) => EditorConfig(
maxScale: 8.0,
cropRectPadding: EdgeInsets.all(20.0),
hitTestSize: 20.0,
),
);
EDIT
editorKey.currentState?.flip();
// or
editorKey.currentState?.rotate();
GET DATA FOR SAVE (as image provider):
ImageProvider<Object> ip = img.image;
Uint8List? editedImageFromProvider = await getImageFromImageProvider(ip);
Future<Uint8List?> getImageFromImageProvider(ImageProvider provider) async {
final ImageStream stream = provider.resolve(ImageConfiguration.empty);
Completer<Uint8List?> completer = Completer<Uint8List?>();
ImageStreamListener? listener;
listener = ImageStreamListener(
(ImageInfo info, bool synchronousCall) {
final Future<ByteData?> byteData = info.image.toByteData(format: ImageByteFormat.png);
byteData.then((value) {
if (value != null) {
completer.complete(value.buffer.asUint8List());
} else {
completer.complete(null);
}
});
// Remove the listener once the image data is fetched
stream.removeListener(listener!);
},
/*onError: (dynamic error, StackTrace stackTrace) {
print('Error occurred while loading image: $error');
completer.complete(null);
stream.removeListener(listener!);
}*/
);
stream.addListener(listener);
return completer.future;
}
USING SIMPLER RAW IMAGE LOGIC
Uint8List? editedImageFromRaw = await getImageFromRaw(img);
Future<Uint8List?> getImageFromRaw(ExtendedImage image) async {
final state = editorKey.currentState;
if (state == null) {
return null;
}
Uint8List result = await state.rawImageData;
return result;
}
With both approaches I get back the same Uint8List which is the one from the original image loaded… With multiple edits (.roatate(), crop, flip()) the data returned remains the same…
I also attempted to remove
cacheRawData: true
from the load as it thought it may be forcing the bad returns. This prevents the state.rawImageData from working, but the image provider still works and returns the same bytecode after each edit.
It looks as if the calls are all working against the original loaded image and not against the edited image.
I should note that the edits are obviously working and visible on screen so the only issue is getting the raw data for the image i can see back to the code.
Thanks…