goh*_*goh 9 ios flutter imagepicker
我正在 Flutter 中开发一个移动应用程序,它需要用户从图库中选择一个图像。
我正在使用 image_picker: ^0.6.7+11 这是我的代码:
if (await Permission.photos.request().isGranted) {
try {
final image =
await ImagePicker().getImage(source: ImageSource.gallery);
if (image != null) {
photo.clearData();
File _image = File(image.path);
photo.addOriginal(_image);
} else {
print('no image selected');
}
} on PlatformException catch (e) {
print('Platform exception $e');
} catch (e) {
print('Unknown error: $e');
}
}
Run Code Online (Sandbox Code Playgroud)
在 android 模拟器中,一切正常。在 iOS 模拟器中,我可以选择一个图像,但如果我尝试选择第二个图像,则应用程序会崩溃并显示“与设备的连接丢失”。打印在运行选项卡中 - 但没有错误。
问题:如何解决此问题,以便我可以返回图库并在 iOS 上根据需要多次选择不同的图像?
调试时我遇到了这个:
PlatformException(multiple_request, Cancelled by a second request, null, null)
Run Code Online (Sandbox Code Playgroud)
我已经在这里、GitHub 等上找到了尽可能多的类似问题......我有:
我确信它是直接的,但我觉得我已经用尽了所有的选择,不知道从哪里开始。
你的代码是正确的,这是一个模拟器问题,如果你想在IOS上测试它,你必须在真正的iPhone上测试,这里是一个关于如何正确执行它的代码片段:
final imagePicker = ImagePicker();
PickedFile pickedFile;
if (fileType == FileType.Camera) {
// Camera Part
pickedFile = await imagePicker.getImage(
source: ImageSource.camera,
maxWidth: 480,
maxHeight: 640,
imageQuality: 25, // pick your desired quality
);
setState(() {
if (pickedFile != null) {
_storedFile = File(pickedFile.path);
} else {
print('No image selected.');
return;
}
});
} else if (fileType == FileType.Gallery) {
// Gallery Part
pickedFile = await imagePicker.getImage(
source: ImageSource.gallery,
maxWidth: 480,
maxHeight: 640,
imageQuality: 25,
);
} else {
print('No image selected.');
return;
}
});
}
Run Code Online (Sandbox Code Playgroud)
当用户选择他想要获取图像的方式时,不要忘记枚举:
enum FileType {
Gallery,
Camera,
Video,
}
Run Code Online (Sandbox Code Playgroud)
编辑:每当您添加依赖于本机代码的包时,您应该重新启动整个应用程序构建,停止构建并再次重新启动它,以便本机代码编译良好,不要忘记在将此包放入 pubspec.yaml 之前重新启动
| 归档时间: |
|
| 查看次数: |
2325 次 |
| 最近记录: |