Flutter 图片选取及裁剪

在开发项目里修改用户头像的功能,涉及到图片选取及裁剪,基本实现步骤如下:

1、pubspec.yaml 添加 image_picker: ^1.0.1 image_cropper: ^4.0.1:

复制代码
dependencies:
  image_picker: ^1.0.1
  image_cropper: ^4.0.1
  flutter:
    sdk: flutter

2、AndroidManifest.xml 添加:

XML 复制代码
<activity
            android:name="com.yalantis.ucrop.UCropActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

3、封装工具类 image_picker_helper.dart ,代码如下:

Dart 复制代码
import 'dart:io';

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

/// @description: 从相册或照相机里选取图片的工具,带裁剪功能
class ImagePickerHelper {
  BuildContext buildContext;

  ImagePickerHelper(this.buildContext);

  void pickImage(ImageSource source, ImagePickerCallback? callback) {
    ImagePicker().pickImage(source: source).then((image) {
      if (image == null) return;
      if (callback == null) return;
      callback.call(image);
    }).onError((error, stackTrace) {
      debugPrint("获取图片失败:$error");
    });
  }

  void cropImage(File originalImage, ImageCropCallback? callback) {
    Future<CroppedFile?> future = ImageCropper().cropImage(
      sourcePath: originalImage.path,
      maxWidth: 100,
      maxHeight: 100,
      uiSettings: [
        AndroidUiSettings(
            toolbarTitle: '',
            toolbarColor: Colors.white,
            statusBarColor: Colors.white,
            toolbarWidgetColor: Colors.green,
            initAspectRatio: CropAspectRatioPreset.square,
            lockAspectRatio: false),
        IOSUiSettings(
          title: '',
        ),
        WebUiSettings(
          context: buildContext,
        ),
      ],
    );
    future.then((value) {
      debugPrint("_cropImage path:${value == null ? "" : value.path}");
      if (value == null) return;
      if (callback == null) return;
      callback.call(value);
    }).onError((error, stackTrace) {
      debugPrint("裁剪图片失败:$error");
    });
  }

  void pickWithCropImage(ImageSource source, ImageCropCallback? callback) {
    pickImage(source, (xFile) {
      cropImage(File(xFile.path), callback);
    });
  }
}

typedef ImagePickerCallback = void Function(XFile xFile);
typedef ImageCropCallback = void Function(CroppedFile croppedFile);

4、使用示例:

Dart 复制代码
//ImageSource.camera 照相机 或 ImageSource.gallery 相册
    ImagePickerHelper(context).pickWithCropImage(ImageSource.gallery,
        (croppedFile) {
      //获取到剪切的文件路径,进行相关的操作
      debugPrint("croppedFile:${croppedFile.path}");
    });
相关推荐
音浪豆豆_Rachel1 天前
Flutter鸿蒙化之深入解析Pigeon非空字段设计:non_null_fields.dart全解
flutter·harmonyos
Zender Han1 天前
Flutter 图片裁剪插件 image_cropper 最新版介绍与使用教程
android·flutter·ios
子榆.1 天前
Flutter 与开源鸿蒙(OpenHarmony)实时音视频通话实战:基于 AVSession 与 Native 音视频栈构建安全通信应用
flutter·开源·harmonyos
xianjixiance_1 天前
Flutter跨平台向量数学库vector_math鸿蒙化使用指南
flutter·华为·harmonyos
消失的旧时光-19431 天前
从命令式跳转到声明式路由:前端、Android、Flutter 的一次统一演进
android·前端·flutter·状态模式
xianjixiance_1 天前
Flutter跨平台UUID生成工具uuid_test鸿蒙化使用指南
flutter·elasticsearch·harmonyos
消失的旧时光-19431 天前
从前端路由到 Android ARouter:观察者模式在不同平台的同一种落地
android·前端·观察者模式·flutter
Zender Han1 天前
Flutter EasyRefresh 最新版本:自定义 Header / Footer 详解与实践
android·flutter·ios
徐安安ye1 天前
Flutter AR 开发:打造厘米级精度的室内导航应用
flutter·ar