如何在本地反应中读取图像

时间:2018-01-16 01:03:27

标签: react-native

如何以反应原生方式从相机胶卷读取图像文件?我尝试使用react-native-fs You can find the issue I am facing using this link来阅读文件。

1 个答案:

答案 0 :(得分:0)

我假设您已经知道如何从CameralRoll中选择图片,只需使用CameraRoll模块中的react-native API即可。如果这是您选择图片的方式,那么您的图片uri应如下所示:asset-library://asset/...

然后,你应该使用react-native-fetch-blob模块:

npm install --save react-native-fetch-blob

react-native link

在定义了uploadImage函数的文件中(我假设您使用firebase作为后端,因为我在您的其他问题中看到了这一点):

import RNFetchBlob from 'react-native-fetch-blob';

const Blob = RNFetchBlob.polyfill.Blob;
const fs = RNFetchBlob.fs;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;

function uploadImage(photo) {
    const ref = firebase.storage().ref('path/to/destination');
    let photoBlob = null;

    let imageFile = RNFetchBlob.wrap(photos[i]);
    Blob.build(imageFile, { type: 'image/jpg' })
        .then((blob) => {
            photoBlob = blob;
            return ref.put(blob, { contentType: 'image/jpg' });
        })
        .then(() => photoBlob.close())
        .catch((error) => console.log(error));
}

全套。但是如果你没有使用CameraRoll来选择图片(uri看起来会有所不同),那就可以了,只需将要上传的照片的路径传递给此功能,并将其他所有内容保持不变