从 CameraRoll 获取图像的 Base64 [英] Getting The Base64 of an Image from CameraRoll

查看:43
本文介绍了从 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆