flutter image crop
image cropping plugin for flutter.
a flutter plugin to crop an image on ios and android.
the plugin comes with a crop
widget. the widget renders only image, overlay, and handles to crop an image. thus it can be composed with other widgets to build custom image cropping experience.
the plugin is working with files to avoid passing large amount of data through method channels. files are stored in cache folders of ios and android. thus if there is a need to save actual croped image, ensure to copy the file to other location.
all of the computation intensive work is done off a main thread via dispatch queues on ios and cache thread pool on android.
note: this plugin is still under development, some features are not available yet and testing has been limited.
installation
add image_crop
as a dependency in pubspec.yaml
.
using
create a widget to load and edit an image:
final cropkey = globalkey<cropstate>();
widget _buildcropimage() {
return container(
color: colors.black,
padding: const edgeinsets.all(20.0),
child: crop(
key: cropkey,
image: image.file(imagefile),
aspectratio: 4.0 / 3.0,
),
);
}
access cropping values:
- scale is a factor to proportionally scale image’s width and height when cropped.
1.0
is no scale needed. - area is a rectangle indicating fractional positions on the image to crop from.
final crop = cropkey.currentstate;
// or
// final crop = crop.of(context);
final scale = crop.scale;
final area = crop.area;
if (area == null) {
// cannot crop, widget is not setup
// ...
}
accessing and workign with images. as a convenience function to request permissions to access photos.
final permissionsgranted = await imagecrop.requestpermissions();
read image options, such as: width and height. this is efficent implementation that does not decode nor load actual image into a memory.
final options = await getimageoptions(file: file);
debugprint('image width: ${options.width}, height: ${options.height}');
if image is large to be loaded into the memory, there is a sampling function that relies on a native platform to proportionally scale down the image befor loading it to the memory. e.g. resample image to get down to 1024x4096
dimension as close as possible. if it is a square preferredsize
can be used to specify both width and height.
final samplefile = await imagecrop.sampleimage(
file: originalfile,
preferredwidth: 1024,
preferredheight: 4096,
);
once crop
widget is ready, there is a native support of croping and scaling an image.
final croppedfile = await imagecrop.cropimage(
file: originalfile,
scale: crop.scale,
area: crop.area,
);
Comments are closed.