Possible Unhandled Promise Rejection (id:0) TypeError: undefined is not an object (evaluating 'ImagePicker.Permissions.askAsync)
Possible Unhandled Promise Rejection (id:0) TypeError: undefined is not an object (evaluating 'ImagePicker.Permissions.askAsync)
我正在使用 React-Native 和 Expo 构建一个 iOS 应用程序。我已经弄清楚如何访问用户的照片库,但无法从他们的设备请求权限。相反,它只是在不询问的情况下访问相机胶卷,这在 iOS 中显然是不允许的。下面是我的一些代码:
import React, {useState, useEffect, useRef} from 'react';
import * as ImagePicker from 'expo-image-picker';
import Permissions, {usePermissions} from 'expo-permissions';
import Constants from 'expo-constants'
//useEffect函数是什么运行检查async函数是否被接受
const Screen = ({navigation}) => {
const [ imagePicker1, setImagePicker1 ] = useState(null);
useEffect(() => {
const permission_get1 = async ()=> {
if (Platform.OS !== 'web'){
let { status } = await ImagePicker.Permissions.askAsync(Permissions.MEDIA_LIBRARY);
if (status !== 'granted'){
console.log('No permission given')
alert('Camera roll required for to upload photo from your library');
return;
}
/*if (status === 'granted'){
console.log('Permission granted')*/
let imagePicker1 = await ImagePicker.getMediaLibraryAsync()
setImagePicker1(imagePicker1)
console.log('It worked')
}
};
permission_get1();
}, []);
我最终使用这个让它工作:我相信前面的例子使用的是折旧方法。
import * as ImagePicker from 'expo-image-picker'
const pickImage = async ()=>{
const { granted } = await Permissions.askAsync(Permissions.CAMERA_ROLL)
if(granted){
console.log('access granted')
let data = await ImagePicker.launchImageLibraryAsync({
mediaTypes:ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
aspect:[1,1],
quality:0.5,
})
console.log(data)
if (!data.cancelled){
setImage(data.uri);
}
}else{
Alert.alert('Permissions required to access camera roll.')
}
}
我正在使用 React-Native 和 Expo 构建一个 iOS 应用程序。我已经弄清楚如何访问用户的照片库,但无法从他们的设备请求权限。相反,它只是在不询问的情况下访问相机胶卷,这在 iOS 中显然是不允许的。下面是我的一些代码:
import React, {useState, useEffect, useRef} from 'react';
import * as ImagePicker from 'expo-image-picker';
import Permissions, {usePermissions} from 'expo-permissions';
import Constants from 'expo-constants'
//useEffect函数是什么运行检查async函数是否被接受
const Screen = ({navigation}) => {
const [ imagePicker1, setImagePicker1 ] = useState(null);
useEffect(() => {
const permission_get1 = async ()=> {
if (Platform.OS !== 'web'){
let { status } = await ImagePicker.Permissions.askAsync(Permissions.MEDIA_LIBRARY);
if (status !== 'granted'){
console.log('No permission given')
alert('Camera roll required for to upload photo from your library');
return;
}
/*if (status === 'granted'){
console.log('Permission granted')*/
let imagePicker1 = await ImagePicker.getMediaLibraryAsync()
setImagePicker1(imagePicker1)
console.log('It worked')
}
};
permission_get1();
}, []);
我最终使用这个让它工作:我相信前面的例子使用的是折旧方法。
import * as ImagePicker from 'expo-image-picker'
const pickImage = async ()=>{
const { granted } = await Permissions.askAsync(Permissions.CAMERA_ROLL)
if(granted){
console.log('access granted')
let data = await ImagePicker.launchImageLibraryAsync({
mediaTypes:ImagePicker.MediaTypeOptions.Images,
allowsEditing: true,
aspect:[1,1],
quality:0.5,
})
console.log(data)
if (!data.cancelled){
setImage(data.uri);
}
}else{
Alert.alert('Permissions required to access camera roll.')
}
}