我使用react-native-image-crop-picker和react-native-fetch-blob从相机胶卷中抓取图像,然后将其保存到Firebase存储桶中。
class CreateForm extends Component {
componentWillMount() {
this.setState({ loading: false });
}
// RNFetchBlob and RNImageCropPicker working together to store an
// image in firebase, and then returning a url for that image.
openImage() {
this.setState({ loading: true });
const Blob = RNFetchBlob.polyfill.Blob;
const fs = RNFetchBlob.fs;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
window.Blob = Blob
//const uid = "12345"
ImagePicker.openPicker({
width: 300,
height: 300,
cropping: true,
mediaType: 'photo',
}).then(image => {
const imagePath = image.path;
let uploadBlob = null;
const storage = firebase.storage();
const storageRef = storage.ref();
const imageRef = storageRef.child('dp.jpg');
const mime = 'image/jpg';
fs.readFile(imagePath, 'base64')
.then((data) => {
//console.log(data);
return Blob.build(data, { type: `${mime};BASE64` });
})
.then((blob) => {
uploadBlob = blob;
return imageRef.put(blob, { contentType: mime });
})
.then(() => {
uploadBlob.close();
return imageRef.getDownloadURL();
})
一切正常,直到这段代码:
.then((url) => {
console.log(url);
this.props.entryUpdate({ prop: 'image', url });
});
});
}
它记录的网址很好。但是,当我尝试将此网址指定为名为' image'对于动作创建者" entryUpdate',Firebase实时数据库获取未定义的对象并引发错误。
此处是动作创建者' entryUpdate':
export const entryUpdate = ({ prop, value }) => {
return {
type: ENTRY_UPDATE,
payload: { prop, value }
};
};
没有'图像'道具,一切都正确执行。所以我知道我并没有以某种方式给动作创建者提供正确的语法。在
this.props.entryUpdate({ prop: 'image', url });
线必须是完全错误的。任何帮助都会非常感激!
答案 0 :(得分:0)
这是有用的:
.then((url) => {
const { image } = this.props;
this.props.entryUpdate({ prop: 'image', value: url });
我必须将{image}声明为this.props,并指定
value: url