-1

我的应用程序中有 react-native-image-picker。如果我正在调用 ImagePicker.launchImageLibrary,则该应用程序正在关闭而不是打开图库。当我调用 ImagePicker.showImagePicker 并在从库中选择.. 选项之后,也会发生同样的行为。该应用程序也正在关闭。我使用官方文档中的相同代码,并在 XCode、连接库和 Pods 中进行了所有安装,没有任何错误。有人可以帮我吗。

import React, { useState } from 'react'
import { View, FlatList, StyleSheet } from 'react-native'
import ImagePicker from 'react-native-image-picker'
import { ChannelItem, ModalChannelCard, SearchBar, ScreenHeader } from '../../components'
import { data } from '../../dataDraft'
import { TOPICS_SCREEN } from '../routes'

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  textStyle: {
    fontSize: 25,
    fontWeight: 'bold'
  }
})

const ChannelsScreen = ({ navigation }) => {
  const { container, textStyle } = styles
  const [isModalVisible, setModalVisible] = useState(false)
  const [newChannelImage, setNewChannelImage] = useState({
    channelImage: null,
    loading: false
  })
  const [newChannelTitle, setNewChannelTitle] = useState('')
  const [userInput, setInput] = useState({
    value: '',
    isEmpty: true
  })
  const onChangeTexthandler = value => {
    setInput({
      value,
      isEmpty: false
    })
  }
  const onPressXButtonHandler = () => {
    setInput({
      value: '',
      isEmpty: true
    })
  }
  const hideModalHandler = () => {
    setModalVisible(false)
  }
  const setChannelImageHandler = ({ source }) => {
    setNewChannelImage({
      channelImage: source,
      loading: true
    })
  }
  const rightIconPressHandler = () => {
    setModalVisible(true)
  }
  const chooseChannelImageHandler = () => {
    const options = {
      title: 'Select Image',
      storageOptions: {
        skipBackup: true,
        path: 'images'
      }
    }
    ImagePicker.showImagePicker(options, response => {
      console.log('Response = ', response)
      if (response.didCancel) {
        console.log('User cancelled image picker')
      } else if (response.error) {
        console.log('ImagePicker error: ', response.error)
      } else if (response.customButton) {
        console.log('User tapped custom button: ', response.customButton)
        alert(response.customButton)
      } else {
        const source = { uri: response.uri }
        setChannelImageHandler(source.uri)
      }
    })
  }

  const newTitleHandler = value => {
    setNewChannelTitle(value)
  }
  return (
    <View style={container}>
      <ScreenHeader header="Channels" rightIconName="ios-add" onRightIconPress={rightIconPressHandler} />
      <SearchBar
        value={userInput.value}
        placeholder="Search channel..."
        isEmpty={userInput.isEmpty}
        onChangeText={onChangeTexthandler}
        onPressXButton={onPressXButtonHandler}
      />
      <ModalChannelCard
        buttonAddChannelPushed={newChannelImage.loading}
        channelImage={newChannelImage.channelImage}
        chooseChannelImage={chooseChannelImageHandler}
        createChannel={hideModalHandler}
        hideModal={hideModalHandler}
        onChangeTitle={newTitleHandler}
        titleValue={newChannelTitle}
        visible={isModalVisible}
      />

      <View style={{ alignItems: 'center' }}>
        <FlatList
          data={data}
          numColumns={2}
          autoCorrect={false}
          keyboardShouldPersistTaps="always"
          keyboardDismissMode="on-drag"
          keyExtractor={item => item.image}
          renderItem={({ item }) => (
            <ChannelItem
              channelItemHeader={item.header}
              imageSource={item.image}
              onPress={() => navigation.navigate(TOPICS_SCREEN)}
            />
          )}
        />
      </View>
    </View>
  )
}

export default ChannelsScreen

4

1 回答 1

0

我找到了解决方案。我没有完成这一步: 注意:如果你没有完成这一步,你会得到一个 SIGABRT 崩溃:

<plist version="1.0">
  <dict>
    ...
    <key>NSPhotoLibraryUsageDescription</key>
    <string>$(PRODUCT_NAME) would like access to your photo gallery</string>
    <key>NSCameraUsageDescription</key>
    <string>$(PRODUCT_NAME) would like to use your camera</string>
    <key>NSPhotoLibraryAddUsageDescription</key>
    <string>$(PRODUCT_NAME) would like to save photos to your photo gallery</string>
    <key>NSMicrophoneUsageDescription</key>
    <string>$(PRODUCT_NAME) would like to use your microphone (for videos)</string>
  </dict>
</plist>
于 2019-07-22T08:51:22.603 回答