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 添加:

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

3、封装工具类 image_picker_helper.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、使用示例:

//ImageSource.camera 照相机 或 ImageSource.gallery 相册
    ImagePickerHelper(context).pickWithCropImage(ImageSource.gallery,
        (croppedFile) {
      //获取到剪切的文件路径,进行相关的操作
      debugPrint("croppedFile:${croppedFile.path}");
    });

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flutter提供了多种方式来进行图片压缩,以下是其中几种常用的方法: 1. 使用flutter_image_compress库:这是一个Flutter插件,可以用于对图片进行压缩。你可以通过在pubspec.yaml文件中添加依赖来使用该库。使用该库的示例代码如下: ```dart import 'package:flutter_image_compress/flutter_image_compress.dart'; Future<void> compressImage(String imagePath) async { final result = await FlutterImageCompress.compressWithFile( imagePath, quality: 85, // 设置压缩质量,范围为0-100 ); if (result != null) { // 压缩成功,result为压缩后的图片数据 // 可以将result保存到文件或上传到服务器 } else { // 压缩失败 } } ``` 2. 使用flutter_native_image库:这是另一个Flutter插件,可以用于对图片进行压缩和调整大小。你可以通过在pubspec.yaml文件中添加依赖来使用该库。使用该库的示例代码如下: ```dart import 'package:flutter_native_image/flutter_native_image.dart'; Future<void> compressImage(String imagePath) async { final compressedFile = await FlutterNativeImage.compressImage( imagePath, quality: 85, // 设置压缩质量,范围为0-100 ); if (compressedFile != null) { // 压缩成功,compressedFile为压缩后的图片文件 // 可以将compressedFile保存到文件或上传到服务器 } else { // 压缩失败 } } ``` 3. 使用第三方云存储服务:你还可以使用第三方云存储服务,如七牛云、阿里云等,它们提供了图片处理的API,可以通过调用API来实现图片压缩。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值