gpt4 book ai didi

image - Flutter/Dart 在实际设备中调整图像大小花费 > 10 分钟

转载 作者:IT王子 更新时间:2023-10-29 06:35:53 25 4
gpt4 key购买 nike

我发现了 Flutter/Dart 的一个行为。我正在尝试调整来自 ImagePicker 的图像的大小。模拟器运行良好,但在实际设备(iPhone 6 plus)上,整个过程花费了 10 多分钟,最后以崩溃告终。

在实际设备上,我单击了调出图像选择器的按钮,选择了一张照片,然后设备就挂起了。 10 分钟后,Image Pciker 关闭并继续调整图像大小,大约 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;
}
}

}

最佳答案

我遇到过类似的问题,即调整图像大小花费的时间太长。我改用 ImagePicker.pickImage 中的 maxHeight 和 maxWidth 参数,并获得了更好的结果。

_imageFile = await ImagePicker.pickImage(
source: ImageSource.gallery,
maxHeight: 450.0,
maxWidth: 450.0);

关于image - Flutter/Dart 在实际设备中调整图像大小花费 > 10 分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50692667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com