与react-native-fbsdk分享图片?

时间:2016-05-05 22:22:17

标签: facebook react-native react-native-fbsdk

我正在尝试通过react-native-fbsdk包在React Native中与Facebook共享图像。

我有以下内容,几乎完全从文档中复制,但我无法弄清楚如何格式化imageUrl,我不断收到错误说SharingContent is invalid

我在这里做错了什么?

const sharePhotoContent = {
  contentType: 'photo',
  photos: [
    {
      imageUrl: "./local/image/path.png",
      userGenerated: false,
      caption: "Hello World"
    }
  ]
};

ShareDialog.canShow(sharePhotoContent).then(
  function(canShow) {
    if (canShow) {
      return ShareDialog.show(sharePhotoContent);
    }
  }
).then(
  function(result) {
    if (result.isCancelled) {
      AlertIOS.alert('Share cancelled');
    } else {
      AlertIOS.alert('Share success with postId: ' + result.postId);
    }
  },
  function(error) {
    AlertIOS.alert('Share fail with error: ' + error);
  }
);

2 个答案:

答案 0 :(得分:1)

您可以使用react-native-image-picker https://github.com/marcshilling/react-native-image-picker获取图像的uri并使用它创建SharePhotoContent。

另请注意,您需要安装Facebook应用才能共享图像。

答案 1 :(得分:-1)

This is working fine for me 

  shareLinkWithShareDialog() {

let shareLinkContent = {

contentType: 'link',

contentUrl: 'https://www.facebook.com/images/fb_icon_325x325.png',

contentDescription: 'Facebook sharing is easy!'
      }

 ShareDialog.canShow(shareLinkContent).then((canShow) => {

if (canShow) return ShareDialog.show(shareLinkContent);

 }

).then((result) => {

 if (result.isCancelled) {

  alert('Share cancelled');

 } else {

   alert('Share success with postId: ' + result.postId);

   }

  },

  function(error) {

  alert('Share fail with error: ' + error);
      }
    );
  }