flutter_image_compress 0.2.2 copy "flutter_image_compress: ^0.2.2" to clipboard
flutter_image_compress: ^0.2.2 copied to clipboard

outdated

compress image with native code(objc kotlin), it's faster. This library can work on android/ios.

flutter_image_compress #

pub package GitHub GitHub stars

compress image with native code(objc kotlin)

This library can work on android/ios.

why #

Q:Dart has image related libraries to compress. Why use native?

A:For reasons unknown, using dart language is not efficient, even in release version, using isolate can not solve the problem.

about android #

maybe, you need update your kotlin version to 1.2.71 or higher.

about ios #

No problems found at present.

use #

dependencies:
  flutter_image_compress: ^0.2.2
import 'package:flutter_image_compress/flutter_image_compress.dart';

use:

see whole example code

  Future<List<int>> testCompressFile(File file) async {
    var result = await FlutterImageCompress.compressWithFile(
      file.absolute.path,
      minWidth: 2300,
      minHeight: 1500,
      quality: 94,
      rotate: 90,
    );
    print(file.lengthSync());
    print(result.length);
    return result;
  }

  Future<File> testCompressAndGetFile(File file, String targetPath) async {
    var result = await FlutterImageCompress.compressAndGetFile(
        file.absolute.path, targetPath,
        quality: 88,
        rotate: 180,
      );

    print(file.lengthSync());
    print(result.lengthSync());

    return result;
  }

  Future<List<int>> testCompressAsset(String assetName) async {
    var list = await FlutterImageCompress.compressAssetImage(
      assetName,
      minHeight: 1920,
      minWidth: 1080,
      quality: 96,
      rotate: 180,
    );

    return list;
  }

  Future<List<int>> testComporessList(List<int> list) async {
    var result = await FlutterImageCompress.compressWithList(
      list,
      minHeight: 1920,
      minWidth: 1080,
      quality: 96,
      rotate: 135,
    );
    print(list.length);
    print(result.length);
    return result;
  }

about List

you maybe need convert List<int> to 'Uint8List' to display image

use Uint8List need import package to your code like this

var u8 = Uint8List.fromList(list)
ImageProvider provider = MemoryImage(Uint8List.fromList(list));

use in Image Widget

    List<int> list = await testCompressFile(file);
    ImageProvider provider = MemoryImage(Uint8List.fromList(list));

    Image(
      image: provider ?? AssetImage("img/img.jpg"),
    ),

write to file

  void writeToFile(List<int> list, String filePath) {
    var file = File(filePath);
    file.writeAsBytes(list, flush: true, mode: FileMode.write);
  }

android build error #

Caused by: org.gradle.internal.event.ListenerNotificationException: Failed to notify project evaluation listener.
        at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:86)
        ...
Caused by: java.lang.AbstractMethodError
        at org.jetbrains.kotlin.gradle.plugin.KotlinPluginKt.resolveSubpluginArtifacts(KotlinPlugin.kt:776)
        ...

see the flutter/flutter/issues#21473

you need edit your kotlin version to 1.2.71+

If flutter supports more platforms (windows, mac, linux, other) in the future and you use this library, propose issue / PR

ABOUT EXIF #

Using this library, EXIF information will be removed.

1290
likes
0
pub points
99%
popularity

Publisher

verified publisherfluttercandies.com

compress image with native code(objc kotlin), it's faster. This library can work on android/ios.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on flutter_image_compress