Flutter / Dart在实际设备中调整图像大小需要10分钟以上

Liv*_*ock 2 image dart flutter

我发现Flutter / Dart有行为。我正在尝试调整ImagePicker中的图像大小。该模拟器运行良好,但在实际设备(iPhone 6 plus)上,整个过程耗时超过10分钟,并以崩溃告终。

在实际的设备上,我单击了带出“图像选择器”的按钮,选择了一张照片,设备刚刚挂起。10分钟后,图像密码器将关闭,并继续调整图像大小,大约5分钟后,图像崩溃。

这是代码:

ImagePicker.pickImage(source: source)
      .then((_imageFile2) => _uploadFile(_imageFile2)
      .then((downbloadURL) {
          if (downbloadURL != null ) {

              createCloudStoreRecord(fireBaseUser, downbloadURL, true);

              setState(() {
                  profileImage = new DecorationImage(
                  image: getProfileImage(downbloadURL), 
                  fit: BoxFit.cover,
                  );

              });
            Navigator.pop(context);
            showInSnackBar("Image Updated");
          } else {
            Navigator.pop(context);
            showInSnackBar("Image Update Error!");
          }
      }));


Future<String> _uploadFile(_imageFile2) async {
    print("in upload image");

    if (_imageFile2==null) {
        print("imagePicker image is null");
        Navigator.pop(context);
        return null;
    } else {
        onLoading(context, "Updating ...");

        try {
          // resize image
          Im.Image image = Im.decodeImage(_imageFile2.readAsBytesSync());
          Im.Image smallerImage = Im.copyResize(image, 500); // choose the size here, it will maintain aspect ratio
          final tempDir = await getTemporaryDirectory();
          final path = tempDir.path;
          var filename = user.uid.toString() + ".png";
          var newPath = '$path/' + filename;
          print("start compressed");
          var compressedImage = new File(newPath)..writeAsBytesSync(Im.encodePng(smallerImage));
          print("end compressed");
          //final Directory systemTempDir = Directory.systemTemp;
          final StorageReference ref = FirebaseStorage.instance.ref().child('users/' + filename);
          final StorageUploadTask uploadTask = ref.putFile(
            compressedImage,
            new StorageMetadata(
              contentLanguage: 'en',
              customMetadata: <String, String>{'renalbase': 'user_photo'},
            ),
          );
          print("Start upload");
          UploadTaskSnapshot uploadSnapshot = await uploadTask.future;

          print("image uploaded");

          Map<String, dynamic> pictureData = new Map<String, dynamic>();
          pictureData["url"] = uploadSnapshot.downloadUrl.toString();
          print("Bfore url = ${pictureData["url"]}");
          final RegExp regExp = RegExp('.*(?=\\?)');
          pictureData["url"] = Uri.decodeFull( regExp.stringMatch(pictureData["url"]) );
          print("url = ${pictureData["url"]}");
          return pictureData["url"];
        } catch(e) {
            print("Upload error: $e");
            showInSnackBar("Upload error: $e");
            return null;
          }
      }

  }
Run Code Online (Sandbox Code Playgroud)

小智 7

我在调整图像大小时遇到​​了类似的问题。我改用 ImagePicker.pickImage 中的 maxHeight 和 maxWidth 参数并获得了更好的结果。

_imageFile = await ImagePicker.pickImage(
          source: ImageSource.gallery, 
          maxHeight: 450.0, 
          maxWidth: 450.0);
Run Code Online (Sandbox Code Playgroud)


Liv*_*ock 5

放弃图像插件,改用flutter_native_image。(https://github.com/btastic/flutter_native_image

奇迹般有效。