Imagepicker React Native在iOS上崩溃但在Android上运行良好

时间:2020-04-03 04:14:02

标签: javascript ios swift react-native

我目前在本机上使用imagepicker。当我使用android选择图像时,它可以正常工作。但是,当我使用iOS时,选择照片会导致崩溃。

这是它在xcode调试器中显示的错误:

2020-04-03 11:54:27.802434 + 0800 app [7218:1993193]-[NSURLResponse allHeaderFields]:无法识别的选择器已发送到实例0x28281aba0 2020-04-03 11:54:27.802766 + 0800 app [7218:1993193] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[NSURLResponse allHeaderFields]:无法识别的选择器已发送至实例0x28281aba0' * 第一个调用堆栈: (0x19d01c164 0x19cd30c1c 0x19cf1a7e0 0x19d02085c 0x19d022b60 0x102b27bc8 0x102b27a90 0x102b01ce0 0x1059f5d10 0x1059f718c 0x1059fe968 0x1059ff580 0x105a0b0f0 014 libc ++ abi.dylib:以类型为NSException的未捕获异常终止

这是我的代码:

chooseImage = async (id) => {
        //await this.askPermissionsAsync();
        let options = {
          title: 'Select Image',
          storageOptions: {
            skipBackup: true,
            path: 'images',
          },
        };
        ImagePicker.showImagePicker(options, (response) => {
          console.log('Response = ', response.error);

          if (response.didCancel) {
            console.log('User cancelled image picker');
          } else if (response.error) {
            console.log('ImagePicker Error: ', response.error);
          } else {
            const source = { uri: response.uri };

            // You can also display the image using data:
            // const source = { uri: 'data:image/jpeg;base64,' + response.data };
            // alert(JSON.stringify(response));


            let file_data = [...this.state.fileData];
            file_data[id] = response.data;

            let file_uri = [...this.state.fileUri];
            file_uri[id] = response.uri;

            this.setState({filePath:response, fileData: file_data, fileUri: file_uri});
          }
        });
      }

我还在info.plist中添加了权限:

    <key>NSCameraUsageDescription</key>
    <string></string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string></string>
    <key>NSPhotoLibraryAddUsageDescription</key>
    <string></string>
    <key>NSDocumentsFolderUsageDescription</key>
    <string></string>

,但是问题仍然存在于ios中。

2 个答案:

答案 0 :(得分:1)

问题是

'data:image/jpeg;base64,' + this.state.fileData[id]

同时在Image标签上呈现它。 此问题显示在这里:

YellowBox Crash

Image Crash with data:image/png;base64

我所做的解决方案是在选项上添加“ noData:true”,然后直接访问image标签中的URI文件位置。

希望这会有所帮助

答案 1 :(得分:0)

react-native链接未链接库,因此法师选择器lib未随该应用程序一起构建。这就是为什么我因为没有lib而得到空指针异常的原因。

或者手动添加库。

<key>NSCameraUsageDescription</key>
	<string>Access to take a photo by camera</string>
<key>NSAppleMusicUsageDescription</key>
	<string>Access to pick a photo</string>
<key>NSPhotoLibraryUsageDescription</key>
	<string>Access to pick a photo</string>