【问题标题】:React Native: react-native-image-picker is not triggering the callback for launchImageLibraryReact Native:react-native-image-picker 没有触发 launchImageLibrary 的回调
【发布时间】:2021-09-18 11:03:51
【问题描述】:

我正在使用 React Native 构建一个移动应用程序。我的应用程序需要访问照片库和相机。我正在使用这个库,https://github.com/react-native-image-picker/react-native-image-picker。我可以启动照片库,但是当我选择照片时,它没有触发回调函数。

这是我的代码:

const renderPreviewImage = () => {
        if (! photoUri) {
            return null
        }

        return (
            <Image
                resizeMode={"cover"}
                resizeMethod={"scale"}
                style={{width: 200, height: 200}}
                source={{uri: photoUri}}
                />
        )
    }

    return (
        <View style={{flex: 1}}>
            <Button label={"Select Photo"} onPress={() => {
                launchImageLibrary({
                    mediaType: "photo"
                }, (response) => {
                    console.log('This is not triggered')
                })
            }} />
            {renderPreviewImage()}
        </View>
    )

正如您在代码中看到的注释,回调没有被调用。我的代码有什么问题,我该如何解决?

【问题讨论】:

    标签: react-native


    【解决方案1】:

    在你的按钮的 onPress 中试试这个:

    let result = await ImagePicker.launchImageLibraryAsync({
          mediaTypes: ImagePicker.MediaTypeOptions.Images,
          allowsEditing: false,
          quality: 1,
        });
    
        if (!result.cancelled) {
          //This is the image being returned
          setImage(result.uri);
        }
    // This how you display the image in your main component below:
        {image && (
                      <Image source={{ uri: image }}
                        style={{ width: '100%', height: '100%' }} />
                    )}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2020-11-04
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多