如何在 React Native 中 return 变量?
How to return variable in React Native?
我有一个应用程序,我可以根据给定的文本和图片制作一个 pdf 文件。
takePicture
相机组件应用程序中的功能拍照,获取照片的宽度和高度,计算比例并将所有信息保存到 AsyncStorage
。它工作正常。
问题是当我试图在不同的组件中获取 AsyncStorage 数据时。
问题是:我怎样才能return这个const height
?我想在 pdf 图像绘图中使用它。尝试了不同的方法,但 none 有效。
getImageHeight = async () => {
try {
const height = await AsyncStorage.getItem('asyncimageHeight')
if (height!== null) {
console.log(`Height from AsyncStorage is: ${height}`)
}
} catch (e) {
alert('Failed to fetch the data from storage')
}
}
getImageHeight();
下面是takePicture
如果它能帮助你理解问题的重点:
takePicture = async() => {
if (this.camera) {
const options = { quality: 0.5, base64: true , fixOrientation: true};
const data = await this.camera.takePictureAsync(options);
console.log(data.uri);
Image.getSize(data.uri, (width, height) => { // Get image width and height of the picture
let imageWidth = width;
let imageHeight = height;
let stringImageWidth = '' + imageWidth; // Makes string for saving in AsyncStorage, allows only strings
let stringImageHeight = '' + imageHeight;
const horizontalRatioCalc = () => { // Calculating image ratio for horizontal pictures
return (imageWidth/imageHeight);
};
const verticalRatioCalc = () => {
return (imageWidth/imageHeight);
};
horizontalImageRatio = horizontalRatioCalc();
verticalImageRatio = verticalRatioCalc();
stringHorizontalImageRatio = '' + horizontalImageRatio;
stringVerticalImageRatio = '' + verticalImageRatio;
console.log(`Size of the picture ${imageWidth}x${imageHeight}`);
horizontalRatio = async () => {
if (imageHeight>imageWidth) {
verticalRatioCalc();
try {
AsyncStorage.setItem("imageVerticalRatio", stringVerticalImageRatio), // Save image ratio, height and width to AsyncStorage
AsyncStorage.setItem("asyncimageWidth", stringImageWidth),
AsyncStorage.setItem("asyncimageHeight", stringImageHeight),
console.log(`Vertical ratio saved! It's ${stringVerticalImageRatio}`),
console.log(`Image Width saved! It's ${stringImageWidth}`),
console.log(`Image height saved! It's ${stringImageHeight}`)
} catch (e) {
console.log(`AsyncStorage saving of image vertical ratio cannot be done.`)
}
}if (imageHeight<imageWidth) {
horizontalRatioCalc();
try {
AsyncStorage.setItem("imageHorizontalRatio", stringHorizontalImageRatio),
AsyncStorage.setItem("asyncimageWidth", stringImageWidth),
AsyncStorage.setItem("asyncimageHeight", stringImageHeight),
console.log(`Horizontal ratio saved! It's ${stringHorizontalImageRatio}`),
console.log(`Image Width saved! It's ${stringImageWidth}`),
console.log(`Image height saved! It's ${stringImageHeight}`)
} catch (e) {
console.log(`AsyncStorage saving of image vertical ratio cannot be done.`)
}
}
}
horizontalRatio();
}, (error) => {
console.error(`Cannot size of the image: ${error.message}`);
});
back(data.uri)
}
};
现在我解决了那个案子,终于太容易了:)
readData = async () => {
try {
kuvakorkeus = await AsyncStorage.getItem('asyncimageHeight')
kuvaleveys = await AsyncStorage.getItem('asyncimageWidth')
vertikaaliratio = await AsyncStorage.getItem('imageVerticalRatio')
horisontaaliratio = await AsyncStorage.getItem('imageHorizontalRatio')
if (kuvakorkeus !== null) {
return kuvakorkeus; // <-- That makes you able to you this variable outside of the function.
}
} catch (e) {
alert('Failed to fetch the data from storage')
}
}
readData ();
我有一个应用程序,我可以根据给定的文本和图片制作一个 pdf 文件。
takePicture
相机组件应用程序中的功能拍照,获取照片的宽度和高度,计算比例并将所有信息保存到 AsyncStorage
。它工作正常。
问题是当我试图在不同的组件中获取 AsyncStorage 数据时。
问题是:我怎样才能return这个const height
?我想在 pdf 图像绘图中使用它。尝试了不同的方法,但 none 有效。
getImageHeight = async () => {
try {
const height = await AsyncStorage.getItem('asyncimageHeight')
if (height!== null) {
console.log(`Height from AsyncStorage is: ${height}`)
}
} catch (e) {
alert('Failed to fetch the data from storage')
}
}
getImageHeight();
下面是takePicture
如果它能帮助你理解问题的重点:
takePicture = async() => {
if (this.camera) {
const options = { quality: 0.5, base64: true , fixOrientation: true};
const data = await this.camera.takePictureAsync(options);
console.log(data.uri);
Image.getSize(data.uri, (width, height) => { // Get image width and height of the picture
let imageWidth = width;
let imageHeight = height;
let stringImageWidth = '' + imageWidth; // Makes string for saving in AsyncStorage, allows only strings
let stringImageHeight = '' + imageHeight;
const horizontalRatioCalc = () => { // Calculating image ratio for horizontal pictures
return (imageWidth/imageHeight);
};
const verticalRatioCalc = () => {
return (imageWidth/imageHeight);
};
horizontalImageRatio = horizontalRatioCalc();
verticalImageRatio = verticalRatioCalc();
stringHorizontalImageRatio = '' + horizontalImageRatio;
stringVerticalImageRatio = '' + verticalImageRatio;
console.log(`Size of the picture ${imageWidth}x${imageHeight}`);
horizontalRatio = async () => {
if (imageHeight>imageWidth) {
verticalRatioCalc();
try {
AsyncStorage.setItem("imageVerticalRatio", stringVerticalImageRatio), // Save image ratio, height and width to AsyncStorage
AsyncStorage.setItem("asyncimageWidth", stringImageWidth),
AsyncStorage.setItem("asyncimageHeight", stringImageHeight),
console.log(`Vertical ratio saved! It's ${stringVerticalImageRatio}`),
console.log(`Image Width saved! It's ${stringImageWidth}`),
console.log(`Image height saved! It's ${stringImageHeight}`)
} catch (e) {
console.log(`AsyncStorage saving of image vertical ratio cannot be done.`)
}
}if (imageHeight<imageWidth) {
horizontalRatioCalc();
try {
AsyncStorage.setItem("imageHorizontalRatio", stringHorizontalImageRatio),
AsyncStorage.setItem("asyncimageWidth", stringImageWidth),
AsyncStorage.setItem("asyncimageHeight", stringImageHeight),
console.log(`Horizontal ratio saved! It's ${stringHorizontalImageRatio}`),
console.log(`Image Width saved! It's ${stringImageWidth}`),
console.log(`Image height saved! It's ${stringImageHeight}`)
} catch (e) {
console.log(`AsyncStorage saving of image vertical ratio cannot be done.`)
}
}
}
horizontalRatio();
}, (error) => {
console.error(`Cannot size of the image: ${error.message}`);
});
back(data.uri)
}
};
现在我解决了那个案子,终于太容易了:)
readData = async () => {
try {
kuvakorkeus = await AsyncStorage.getItem('asyncimageHeight')
kuvaleveys = await AsyncStorage.getItem('asyncimageWidth')
vertikaaliratio = await AsyncStorage.getItem('imageVerticalRatio')
horisontaaliratio = await AsyncStorage.getItem('imageHorizontalRatio')
if (kuvakorkeus !== null) {
return kuvakorkeus; // <-- That makes you able to you this variable outside of the function.
}
} catch (e) {
alert('Failed to fetch the data from storage')
}
}
readData ();