flutter - 如何在 flutter 中从图像选择器中裁剪图像?

标签 flutter dart imagepicker

我想让用户裁剪他从图片选择器中选择的图片并保存(类似于在 Whatsapp 上上传个人资料图片)。我怎样才能在 flutter 中做到这一点?

示例图片:

enter image description here

最佳答案

你可以使用这两个 flutter 库来实现这一点,

  1. 图像选择器;
  2. image_cropper

图像选择器使用 iOS 和 Android 中的原生库来实现它,因此它将提供良好的性能方式。

这是 image_picker 中可用的示例:

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';

import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';

void main() => runApp(new ConfigScreen());

class ConfigScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ImageCropper',
      theme: ThemeData.light().copyWith(primaryColor: Colors.deepOrange),
      home: MyHomePage(
        title: 'ImageCropper',
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

  MyHomePage({this.title});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

enum AppState {
  free,
  picked,
  cropped,
}

class _MyHomePageState extends State<MyHomePage> {
  AppState state;
  File imageFile;

  @override
  void initState() {
    super.initState();
    state = AppState.free;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: imageFile != null ? Image.file(imageFile) : Container(),
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.deepOrange,
        onPressed: () {
          if (state == AppState.free)
            _pickImage();
          else if (state == AppState.picked)
            _cropImage();
          else if (state == AppState.cropped) _clearImage();
        },
        child: _buildButtonIcon(),
      ),
    );
  }

  Widget _buildButtonIcon() {
    if (state == AppState.free)
      return Icon(Icons.add);
    else if (state == AppState.picked)
      return Icon(Icons.crop);
    else if (state == AppState.cropped)
      return Icon(Icons.clear);
    else
      return Container();
  }

  Future<Null> _pickImage() async {
    imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
    if (imageFile != null) {
      setState(() {
        state = AppState.picked;
      });
    }
  }

  Future<Null> _cropImage() async {
    File croppedFile = await ImageCropper.cropImage(
        sourcePath: imageFile.path,
        aspectRatioPresets: Platform.isAndroid
            ? [
                CropAspectRatioPreset.square,
                CropAspectRatioPreset.ratio3x2,
                CropAspectRatioPreset.original,
                CropAspectRatioPreset.ratio4x3,
                CropAspectRatioPreset.ratio16x9
              ]
            : [
                CropAspectRatioPreset.original,
                CropAspectRatioPreset.square,
                CropAspectRatioPreset.ratio3x2,
                CropAspectRatioPreset.ratio4x3,
                CropAspectRatioPreset.ratio5x3,
                CropAspectRatioPreset.ratio5x4,
                CropAspectRatioPreset.ratio7x5,
                CropAspectRatioPreset.ratio16x9
              ],
        androidUiSettings: AndroidUiSettings(
            toolbarTitle: 'Cropper',
            toolbarColor: Colors.deepOrange,
            toolbarWidgetColor: Colors.white,
            initAspectRatio: CropAspectRatioPreset.original,
            lockAspectRatio: false),
        iosUiSettings: IOSUiSettings(
          title: 'Cropper',
        ));
    if (croppedFile != null) {
      imageFile = croppedFile;
      setState(() {
        state = AppState.cropped;
      });
    }
  }

  void _clearImage() {
    imageFile = null;
    setState(() {
      state = AppState.free;
    });
  }
}

关于flutter - 如何在 flutter 中从图像选择器中裁剪图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50343045/

相关文章:

flutter - android 应用程序中的 image_picker 崩溃 - Flutter 应用程序

flutter - 当按钮位于父级时,如何从子级到父级获得值(value)?

firebase - 如何使用 Flutter/Dart 在 FirebaseDatabase 中获取值的键?

ios - 尝试为 iOS 构建 flutter 应用程序时出现问题

flutter - 类型 'FutureBuilder<File>'不是 'ImageProvider<dynamic>'类型的子类型-图像选择器问题

api - 如何使用 HTTP 将多张图片上传到 Flutter 中的 Rest API?

dart - flutter - 异步函数总是返回 null,而它不是 null

dart - 为什么通过 BuildContext 会说该 Context 中没有 Scaffold?

dart - 使用Dart gcloud API连接到gcd数据存储

firebase - 使用Dart迭代DataSnapshot