fast_image_resizer

这个包使用原生实现来调整图片大小。它应该
比 image 包更快。
目前仅支持 PNG 作为输出。

这个包没有依赖项。

功能

  • 支持的输入:JPEG, PNG, GIF, 动画 GIF, WebP, 动画 WebP, BMP, 和 WBMP
  • 支持的输出:PNG

入门

将 fast_image_resizer 添加到你的 pubspec.yaml

用法

import 'package:fast_image_resizer/fast_image_resizer.dart';
import 'dart:typed_data';

final rawImage = await rootBundle.load('assets/someImage.png');
final bytes = await resizeImage(Uint8List.view(rawImage.buffer), width: 150);
if (bytes != null) {
    final imageWidget = Image.memory(Uint8List.view(bytes.buffer));
    showDialog(
        context: context,
        builder: (BuildContext context) {
            return AlertDialog(
            title: const Text("Image"),
            content: imageWidget,
            );
        }
    );
}

或者

final ImagePicker _picker = ImagePicker();
final XFile? image =
    await _picker.pickImage(source: ImageSource.gallery);
if (image != null) {
    final rawImage = await image.readAsBytes();
    final bytes = await resizeImage(Uint8List.view(rawImage.buffer), height: 250);
    if (bytes != null) {
        final testing = Image.memory(Uint8List.view(bytes.buffer));
        showDialog(
            context: context,
            builder: (BuildContext context) {
                return AlertDialog(
                    title: Text("Image"),
                    content: testing,
                );
            }
        );
    }
}

GitHub

查看 Github