从 CameraRoll 获取图像的 Base64

编程入门 行业动态 更新时间:2024-10-22 09:40:15
本文介绍了从 CameraRoll 获取图像的 Base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从 CameraRoll 获取图像的 Base64,以便将其传递给第三方系统.我不想使用 RN-Fetch-Blob.我试过 ImageStore,但它说我提供了一个无效的 URI.

I'm trying to get the Base64 of an image from CameraRoll so I can pass it along to a third-party system. I don't want to use RN-Fetch-Blob. I've tried ImageStore, but it says I'm supplying an invalid URI.

解决这个问题的最佳方法是什么?React-Native 有办法吗?还是我必须自己写一些东西?

What's the best way to go about this? Does React-Native have the means? Or do I have to write something natively?

推荐答案

以防万一其他人最终遇到同样的问题....

Just in case someone else ends up with the same issue....

我最终完全没有使用 RN-Fetch-Blob.尽管 react-native 没有提供直接访问图像 base64 的方法,但您可以通过 ImageEditor 裁剪图像来访问它.提取图像宽度和高度,然后裁剪相同尺寸的图像.这将图像移动到本地存储 (ImageStore),一旦裁剪,就提供了一种提取图像 base64 的方法.像这样...

I ended up not using RN-Fetch-Blob at all. Although react-native doesn't provide a way to access the base64 of an image directly, you can access it by cropping the image via ImageEditor. Extract the images width and height, and then crop the image with the same dimensions. This moves the image to the local storage (ImageStore), and once cropped, provides a method for extracting the images' base64. Like so...

_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 很快会将此功能添加到他们的框架中.

It's not pretty, but it does work. Hopefully React-Native will be adding this functionality to their framework soon.

更多推荐

从 CameraRoll 获取图像的 Base64

本文发布于:2023-07-27 00:12:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   CameraRoll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!