【问题标题】:Getting The Base64 of an Image from CameraRoll从相机胶卷中获取图像的 Base64
【发布时间】:2018-08-11 19:17:31
【问题描述】:
我正在尝试从 CameraRoll 获取图像的 Base64,以便将其传递给第三方系统。我不想使用 RN-Fetch-Blob。我试过 ImageStore,但它说我提供的 URI 无效。
解决此问题的最佳方法是什么? React-Native 有办法吗?还是我必须自己写一些东西?
【问题讨论】:
标签:
react-native
react-native-android
react-native-ios
react-native-image
【解决方案1】:
以防万一其他人最终遇到同样的问题....
我最终根本没有使用 RN-Fetch-Blob。尽管 react-native 没有提供直接访问图片 base64 的方法,但您可以通过 ImageEditor 裁剪图片来访问它。提取图像的宽度和高度,然后以相同的尺寸裁剪图像。这会将图像移动到本地存储 (ImageStore),一旦裁剪,就提供了一种提取图像 base64 的方法。就这样……
_selectImage = (photo) => () => {
const { height, uri, width } = photo.node.image
ImageEditor.cropImage(uri,
{
offset: { x: 0, y: 0 },
size: { width: width, height: height },
},
(uri) => {
this._getBase64(uri)
},
(failure) => {
Alert.alert("Error: Unable to attach image")
})
}
_getBase64 = (uri) => {
ImageStore.getBase64ForTag(uri,
(base64) => {
this._onImageSelected(base64, uri)
},
(failure) => {
Alert.alert("Error: Unable to secure image data")
}
)
}
它不漂亮,但它确实有效。希望 React-Native 很快就会将此功能添加到他们的框架中。