我正在开发一个 React Native 项目,在该项目中我使用 CameraRoll 库访问设备图库中的照片。我的要求是确定每张照片最初是横向还是纵向模式拍摄的。
我已经使用 CameraRoll.getPhotos() 成功获取了照片,但返回的对象似乎没有提供有关照片原始方向(横向或纵向)的明确信息。这是我得到的数据的示例:
{
"extension": "jpg",
"fileSize": 1057317,
"filename": "example.jpg",
"height": null,
"orientation": 0,
"playableDuration": null,
"uri": "file:///example-path",
"width": null
}
如您所见,宽度和高度返回 null,我不知道如何使用方向字段来确定照片的原始模式。
有没有办法准确判断每张照片是使用CameraRoll以横向还是纵向模式拍摄的?如有必要,我愿意使用其他库。
感谢您的协助。
我不熟悉
CameraRoll
,但如果你愿意,你可以利用Image.getSize
或Image.resolveAssetSource
方法
这是一个
Hook
使用 Image.getSize
方法的示例 [未经测试]
import {Image} from 'react-native';
const imageOrientation = (source) => {
const [Orientation, setOrientation] = React.useState(null);
React.useEffect(() => {
Image.getSize(
source,
(widthSize, heightSize) => {
if (widthSize > heightSize) setOrientation('landscape')
else setOrientation('portrait')
},
(err) => {
console.error(err);
}
);
}, [source]);
return Orientation
};
以后就可以像这样使用了
const orientationImage = imageOrientation(uriOfTheImage)
React.useEffect(() => {
console.log('orientationImage => ', orientationImage)
}, [orientationImage]);