使用 React Native 将图像分享到社交媒体

Share images to social media using react native

我正在开发一个 React Native 应用程序。我想将我从 firebase 存储中获取并在应用程序中列出的选定图像分享到 WhatsApp 等社交媒体。为此,我使用了一个名为

的 npm 包

react-native-share

。使用它我可以共享文本但不能共享图像。官方页面告诉我应该先将图像转换为 base64,我已经这样做了,应用程序开始崩溃。谁能告诉我怎么做。

您不再需要使用 react-native-share。使用本机 Share 组件。

检查此 post:React Native - can we share an image and text into whatsapp?

您可以在此处找到代码示例:

import React, { Component } from 'react';
import {
  Share,
  Text,
  TouchableOpacity
} from 'react-native';

const shareOptions = {
  title: 'Title',
  message: 'Message to share', // Note that according to the documentation at least one of "message" or "url" fields is required
  url: 'www.example.com',
  subject: 'Subject'
};

export default class ShareExample extends React.Component {

  onSharePress = () => Share.share(shareOptions);

  render(){
    return(
      <TouchableOpacity onPress={this.onSharePress} >
        <Text>Share data</Text>
      </TouchableOpacity>
    );
  }
}

最后,您必须选择发送图片 + 短信的选项:- 您可以使用 shareOptions 的 url 字段添加图片的远程 URI,以便可以在 WhatsApp 消息中预览它,并且标题或主题字段以添加文本。 - 您可以像这样共享 base64 文件 url:url:'data:image/png;base64,'