In Flutter/Dart, how can I perform the following 3 steps:
- Read an image from disk,
- Read its original dimensions (width and height),
- Resize it.
Note: I must be able to display the final result with a regular Flutter Image widget.
CLARIFICATION: I don't want to save the image, but I do want to actually resize it in memory.
You can read image from the disk using the image.file constructor.
For more features you can use the Image library
Sample from the documentation examples
You can use the dart
image
package: https://pub.dartlang.org/packages/image.The package provide various services such as resize, crop and rotate.
While this package does work, unfortunately it is very slow.
See discussion: https://github.com/brendan-duncan/image/issues/55
To resize an image that is defined in pubspec.yaml use "BoxFit":
also reference how to access images: https://flutter.io/assets-and-images/
It's not a very good way to resize picture via Image library, since it blocks ui thread, and it brings very bad UX. There is a a
maxWidth
argument inimage_picker
lib, you can set it, so these writing files manipulation will be unnecessary in some cases.you can use the image class from dart ui library, get the image object with your desired width and height using the frameInfo from intantiateImageCodec and then save it in your desired path
Here's an example
Thumbnail
widget which does this on the flightIt uses
Isolate
to offload CPU-intensive work to background thread and have UI thread jank-free