React native 从uri获取高度和宽度
我有一个应用程序,可以拍摄照片并将其添加到pdf文件中。问题是调整大小。我可以用像素调整它的大小,但不能保持原来的比例。我需要原始的高度和宽度来计算正确的尺寸 计算:将高度除以宽度,例如1200/1600=0,75。然后我们可以调整pd图像高度100px,宽度为100/0,75 问题是:如何获得图像的大小React native 从uri获取高度和宽度,react-native,react-native-android,React Native,React Native Android,我有一个应用程序,可以拍摄照片并将其添加到pdf文件中。问题是调整大小。我可以用像素调整它的大小,但不能保持原来的比例。我需要原始的高度和宽度来计算正确的尺寸 计算:将高度除以宽度,例如1200/1600=0,75。然后我们可以调整pd图像高度100px,宽度为100/0,75 问题是:如何获得图像的大小(data.uri) 我的代码: takePicture = async() => { if (this.camera) { const options
(data.uri)
我的代码:
takePicture = async() => {
if (this.camera) {
const options = { quality: 0.5, base64: true , fixOrientation: true}; // Option for wuality etc
const data = await this.camera.takePictureAsync(options); // Take the image
console.log(data.uri); // Prints image (data) address to console log
back(data.uri) // Go back to ReportFault page
}
};
您可以使用react native
Image
getSize方法
试试这个
import { Image } from 'react-native';
.....
Image.getSize(uri, (width, height) => {
console.log(`The image dimensions are ${width}x${height}`);
}, (error) => {
console.error(`Couldn't get the image size: ${error.message}`);
});
谢谢你Mehran,现在它工作了。今天我会在6个小时内找到解决方案。我头疼,真的。谢谢
import { Image } from 'react-native';
.....
Image.getSize(uri, (width, height) => {
console.log(`The image dimensions are ${width}x${height}`);
}, (error) => {
console.error(`Couldn't get the image size: ${error.message}`);
});