3

我希望我的 react-native 应用与 Instagram 分享照片。我知道在编写本机代码时可以使用指定的照片打开 Instagram 的过滤器屏幕。一个限制是我使用的是 Expo SDK,它不允许对本机依赖项使用“npm link”。

调用此函数时,它将打开 Instagram:

  _handlePress = () => {
    Linking.openURL('instagram://app');
  }

这是按钮:

   <Button 
      onPress={this._handlePress}
      title='Open Instagram'
    />

图像存储在以下状态:

  state = {
    image: null,
    uploading: false
  }

我可以很好地在 Image 标签中显示图像:

<Image
  source={{uri: image}}
  style={{width: 300, height: 300}}
 />

但是,我无法将图像传递给 Instagram。这是一些失败的研究:第三方库:react-native-instagram-share:https ://www.instagram.com/developer/mobile-sharing/iphone-hooks/

因为我不能使用“链接”来使用本机依赖项。我正在使用 Expo SDK,它不允许本地依赖项的“链接”。

Instagram 文档解释了如何打开 Instagram,并且可以使用本机代码来传递图像。其中,是使用“UIDocumentInteractionController”,这在 JavaScript 中是不可用的。
https://www.instagram.com/developer/mobile-sharing/iphone-hooks/

我的问题没有任何其他 Stackoverflow 答案。

4

4 回答 4

16

我在这里整理了一个可以在 iOS 上运行的示例:https ://snack.expo.io/rkbW-EG7-

完整代码如下:

import React, { Component } from 'react';
import { Linking, Button, View, StyleSheet } from 'react-native';
import { ImagePicker } from 'expo';

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Button title="Open camera roll" onPress={this._openCameraRoll} />
      </View>
    );
  }

  _openCameraRoll = async () => {
    let image = await ImagePicker.launchImageLibraryAsync();
    let { origURL } = image;
    let encodedURL = encodeURIComponent(origURL);
    let instagramURL = `instagram://library?AssetPath=${encodedURL}`;
    Linking.openURL(instagramURL);
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
  },
});

这将让您打开相机胶卷并选择一张图片,然后打开 Instagram 应用程序并选择该图片。如果图像尚未在相机胶卷上,那么您可以CameraRoll.saveToCameraRoll在图像上使用(链接到 React Native 文档)来获取它。

但是,此示例中使用的方法仅在您可以从相机胶卷中共享时才有效,但是我不确定如何在 Android 上执行此操作。我在 Expo 的功能请求板上提出了一个问题,以确保 Instagram 共享开箱即用。

于 2017-06-17T04:37:57.947 回答
3

以下是如何从远程 URL 共享文件:首先下载它!:)

  const downloadPath = FileSystem.cacheDirectory + 'fileName.jpg';
  // 1 - download the file to a local cache directory
  const { uri: localUrl } = await FileSystem.downloadAsync(remoteURL, downloadPath);
  // 2 - share it from your local storage :)
  Sharing.shareAsync(localUrl, {
    mimeType: 'image/jpeg',            // Android
    dialogTitle: 'share-dialog title', // Android and Web
    UTI: 'image/jpeg'                  // iOS
  });
于 2019-08-06T15:42:16.590 回答
2

这是你发帖的方式

import { CameraRoll } from 'react-native'

callThisFunction = async () => {
      await CameraRoll.saveToCameraRoll("https://images.unsplash.com/photo-1504807417934-b7fdec306bfd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80", 'photo');
      let instagramURL = `instagram://library?AssetPath=null`;
      Linking.openURL(instagramURL);
  }
于 2019-02-28T01:09:18.713 回答
0

为我工作:)

onClick = () => {
  ImagePicker.launchImageLibrary(options, response =>{
    console.log(response);
    let instagramURL = `instagram://library?LocalIdentifier=`+response.origURL;
    Linking.openURL(instagramURL);
  })
于 2019-10-20T19:23:09.833 回答