flutter_image_compress 0.1.1 flutter_image_compress: ^0.1.1 copied to clipboard
compress image with native code(objc kotlin), it's faster.
flutter_image_compress #
compress image with native code(objc kotlin)
why #
Q:Dart has image related libraries to compress. Why use native?
A:For efficiency reasons, the compression efficiency of some dart libraries is not high, and it will be stuck to UI, even if isolate is used.
use #
dependencies:
flutter_image_compress: ^0.1.0
import 'package:flutter_image_compress/flutter_image_compress.dart';
compress with file path
var img = AssetImage("img/img.jpg");
print("pre compress");
var config = new ImageConfiguration();
AssetBundleImageKey key = await img.obtainKey(config);
final ByteData data = await key.bundle.load(key.name);
File file = File("test.png");
file.writeAsBytesSync(data.buffer.asUint8List());
var result = await FlutterImageCompress.compressWithFile(
file.absolute.path, // file path
minWidth: 2300,
minHeight: 1500,
quality: 94,
); // compress
print(file.lengthSync());
print(result.length);
compress with list
Future<void> compress() async {
var img = AssetImage("img/img.jpg");
print("pre compress");
var config = new ImageConfiguration();
AssetBundleImageKey key = await img.obtainKey(config);
final ByteData data = await key.bundle.load(key.name);
var beforeCompress = data.lengthInBytes;
print("beforeCompress = $beforeCompress");
var result =
await FlutterImageCompress.compressWithList(
data.buffer.asUint8List(), //list
minWidth: 2300,
minHeight: 1500,
quality: 94,
);
print("after = ${result?.length ?? 0}");
}