Native Flutter Image tools
This plugin aims to have native tools to resize images and reduce their quality by compression. The code is somewhat hacky (especially the iOS part), but it works for my needs and hasn't crashed on me. Feel free to improve it if you want to.
Right now there are a few functions. Please find some examples below.
Add the following lines to your pubspec.yaml under dependencies
flutter_native_image: ^0.0.6
File compressedFile = await FlutterNativeImage.compressImage(file.path,
quality: quality, percentage: percentage);
You have to give it a file from the file system and optionally provide a quality (1-100) and a resizing percentage (1-100). Each platform will use it's proper tools to handle the resizing.
To resize the image to the certain size, use following code:
ImageProperties properties = await FlutterNativeImage.getImageProperties(file.path);
File compressedFile = await FlutterNativeImage.compressImage(file.path, quality: 80,
targetWidth: 600, targetHeight: 300);
Keep aspect ratio of the file:
ImageProperties properties = await FlutterNativeImage.getImageProperties(file.path);
File compressedFile = await FlutterNativeImage.compressImage(file.path, quality: 80,
targetWidth: 600,
targetHeight: (properties.height * 600 / properties.width).round());
ImageProperties properties = await FlutterNativeImage.getImageProperties(file.path);
It returns an ImageProperties object containing the width and the height of the image.
File croppedFile = await FlutterNativeImage.cropImage(file.path, originX, originY, width, height);
Returns a file containing the image cropped with the given dimensions.
Alexis Leblond (a-leblond) the image properties feature.
Eugene Strokin the resize to target height/width feature
Dae Won Kim null-safety feature
Shoutouts to Trevor from Vocaro.com. He had the fitting algorithm for resizing images in Objective-C.
Source: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
For preserving exif information, I took the code from googles image_picker github (https://github.com/flutter/plugins/tree/master/packages/image_picker)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。