繁体   English   中英

Flutter Image_Picker 在打开相机时崩溃 iOS 应用程序

[英]Flutter Image_Picker crash iOS app when open Camera

当我打开图库或相机时,一切在 Android 中运行完美,但当我尝试 iOS(模拟器和真实设备)时,图库运行完美,但一旦我按下相机选项,应用程序崩溃而没有控制台日志,只有“与设备的连接丢失”

 // get image File camera
 _getFromCamera(BuildContext context) async {
   XFile? pickedFile = await ImagePicker().pickImage(
     source: ImageSource.camera,
     maxWidth: 1800,
     maxHeight: 1800,
   );
   File rotatedImage =
       await FlutterExifRotation.rotateAndSaveImage(path: pickedFile!.path);
   if (pickedFile != null) {
     image = rotatedImage;
     //File(pickedFile.path);
     final userId = widget.id;
     widget.bloc.uploadProfilePicture(image, userId);
   }
 }

Then I called the function in a Icon button

TextButton.icon(
                     icon: Icon(
                       Icons.photo_camera,
                       color: Theme.of(context).primaryColor,
                     ),
                     label: Text(
                       AppLocalization.of(context)!
                           .getTranslatedValues("cameraLbl")!,
                       style: TextStyle(
                           color: Theme.of(context).colorScheme.secondary,
                           fontWeight: FontWeight.bold),
                     ),
                     onPressed: () {
                       _getFromCamera(context);
                       Navigator.of(context).pop();
                     },
                   )

您必须在 info.plist 文件中添加额外的权限

在其中添加这些条目:

<key>NSPhotoLibraryUsageDescription</key>
<string>Needs PhotoLib access</string>

<key>NSCameraUsageDescription</key>
<string>Needs camera access</string>

要在 flutter 应用程序中添加权限,您需要在 android 清单和 info.plist 文件中添加权限。

您也可以在字符串标签内使用任何字符串,该字符串将作为用户需要授予权限的原因显示给用户。

在 info.plist 文件中添加权限密钥(位于 {app_folder}/ios/Runner/Info.plist

这是位于 info.plist 文件

并添加

<key>NSPhotoLibraryUsageDescription</key>
<string>Needs PhotoLib access</string>

<key>NSCameraUsageDescription</key>
<string>Needs camera access</string>

这将显示相机和图库的权限 window

提示:热重载是不够的! 按“停止调试”和“开始调试”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM