【问题标题】:Flutter Resize ImagePicker Image before uploading to firebaseFlutter Resize ImagePicker Image 在上传到firebase之前
【发布时间】:2021-02-07 10:52:31
【问题描述】:

我正在使用 Flutter 构建一个演示壁纸应用程序,用户可以将图像上传到 Firebase。加载这些图像时,我首先要加载图像的小版本,并且只有在用户单击图像后,才加载完整版本。 为了实现这一点,我想一旦用户选择图像,我只需在后台上传 2 个版本。现在我正在努力实现这一目标。

以下是用户如何使用 ImagePicker 将图像挑选到文件 var 中。

Future pickImage() async {
var tempImage = await ImagePicker.pickImage(source: ImageSource.gallery, maxHeight: 2000);
print(tempImage.runtimeType);


setState(() {
  inspirationimage = tempImage;
});
String result = await uploadImage();
}

如您所见,tempimage 是完整大小的版本。我现在会有这样的东西:

var smallImage = tempImage.resize(height: 200);

显然这不起作用,因为 tempImage 是文件类型。任何想法通常如何解决?

谢谢

【问题讨论】:

    标签: flutter dart imagepicker


    【解决方案1】:

    由于您使用的是 Firebase,因此您可以使用 Firebase "Resize Image" extension。但是,如果您想在客户端执行此操作,请查看 this answer

    【讨论】:

    【解决方案2】:

    你为什么不让你的用户使用这个来裁剪图像 image_cropper 1.3.1 插件? 链接:https://pub.dev/packages/image_cropper

    例子

    // File image = await ImagePicker.pickImage(source: source);
        final _picker = ImagePicker();
        PickedFile image = await _picker.getImage(source: source);
        if (image != null) {
          // Remove crop attribute if we don't want to resize the image
          File cropped = await ImageCropper.cropImage(
            sourcePath: image.path,
            aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
            compressQuality: 100, // 100 means no compression
            maxWidth: 700, 
            maxHeight: 700,
            compressFormat: ImageCompressFormat.jpg,
            androidUiSettings: AndroidUiSettings(
              toolbarColor: primaryColor,
              toolbarTitle: "RPS Cropper",
              statusBarColor: primaryColor,
              backgroundColor: Colors.white,
              //toolbarWidgetColor: primaryColor,
              activeControlsWidgetColor: primaryColor,
              //dimmedLayerColor: primaryColor,
              cropFrameColor: primaryColor,
              cropGridColor: primaryColor,
            ),
          );
    

    【讨论】:

    • 这个我其实也用过
    猜你喜欢
    • 1970-01-01
    • 2010-11-21
    • 2021-10-13
    • 2021-01-03
    • 2020-12-27
    • 2020-12-18
    • 2021-06-10
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多