微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我在ios中上传图像时,expo-image-picker给出网络请求失败,但在android上工作正常

如何解决当我在ios中上传图像时,expo-image-picker给出网络请求失败,但在android上工作正常

这是我用来在IOS上上传图像的代码

  let result = await ImagePicker.launchImageLibraryAsync({
                    mediaTypes: ImagePicker.MediaTypeOptions.Image,allowsEditing: true,quality: 1,});

输出中我得到了

Object {
  "cancelled": false,"height": 2001,"type": "image","uri": "file:///Users/sajid/Library/Developer/CoreSimulator/Devices/A48F2141-D9AF-457A-9D14-D2F2D4B6336B/data/Containers/Data/Application/803F58D8-0CF9-43CA-8972-599C460687B1/Library/Caches/ExponentExperienceData/%2540sajid_542%252FGoTrillo/ImagePicker/19952322-2D5E-495B-A4AF-427EA23663A6.jpg","width": 3000,}

到现在为止,它工作正常,稍后当我在result.uri上调用fetch时,它在ios上给了我以下错误

const response = await fetch(result.uri);

输出错误:TypeError:网络请求失败

错误不是在Android上引发的,因为当我第二次使用上述功能时,在IOS中会给出此错误。请提出针对此问题的修正。

解决方法

在Google上进行大量搜索后,我找到了解决方案,

我们需要用/ private替换file://来修复ios物理设备中的问题。

You can refer this link for extra inforamtion

,

Expo Team处理修补本地版本的补丁时,有另一个解决此问题的方法。对于iOS,您可以使用此implementation,对于Android,您可以使用常规XHR足够详细的问题here

,

我遇到同样的问题很长时间了。这就是我所做的。

请确保按照放大指南设置应用。放大初始化,放大添加身份验证,放大推送,然后放大添加存储,然后执行此操作。

import Amplify,{ Storage } from 'aws-amplify'
import config from './src/aws-exports'
// import awsconfig from './aws-exports';
// Might need to switch line 7 to awsconfig 
Amplify.configure(config)

import { StatusBar } from 'expo-status-bar';
import React,{ useState,useEffect } from 'react';
import { Button,Image,View,Platform,StyleSheet,Text,TextInput } from 'react-native';
import * as ImagePicker from 'expo-image-picker';

function App() {
  const [image,setImage] = useState(null)
  const [name,setName] = useState('Evan Erickson')

  useEffect(() => {
    (async () => {
      if (Platform.OS !== 'web') {
        const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
        if (status !== 'granted') {
          alert('Sorry,we need camera roll permissions to make this work!');
        }
      }
    })();
  },[]);

  const pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,allowsEditing: true,aspect: [4,3],quality: 1,});
    
    console.log(result) 

    async function pathToImageFile(data) {
      try {
        const response = await fetch(data);
        const blob = await response.blob();
        await Storage.put(`customers/${name}`,blob,{
          contentType: 'image/jpeg',// contentType is optional
        });
      } catch (err) {
        console.log('Error uploading file:',err);
      }
    }
    // later
    pathToImageFile(result.uri);
  }

  return (
    <View style={{ flex: 1,alignItems: 'center',justifyContent: 'center' }}>
      <Button title="Pick an image from camera roll" onPress={pickImage} />
      {image && <Image source={{ uri: image }} style={{ width: 200,height: 200 }} />}
      <Button title="Upload image" onPress={() => {alert(image)}} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,backgroundColor: '#fff',justifyContent: 'center',},});

export default withAuthenticator(App)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。