首页 > 解决方案 > 如何在 React Native 中将远程 GIF 保存到 iOS 中的相册?

问题描述

感谢阅读,我正在尝试将远程 GIF 保存到 iOS 中 React Native 中的系统相册。

我尝试了以下方法,但它们都将 GIF 保存为静态图像。

我创建了一个 0.62.2 的新 React Native 项目来运行这些代码

  "dependencies": {
    "@react-native-community/cameraroll": "^1.7.2",
    "react": "16.11.0",
    "react-native": "0.62.2",
    "react-native-fs": "^2.16.6",
    "rn-fetch-blob": "^0.12.0"
  }
  1. 通过 CameraRoll 直接保存 GIF。
    不工作,因为https://github.com/react-native-community/react-native-cameraroll/issues/16
const uri = 'https://i.loli.net/2020/05/28/YKaCdO1pexuThbN.gif'
CameraRoll.saveToCameraRoll(uri);
  1. 首先通过 react-native-fs 下载 GIF
const img = 'https://i.loli.net/2020/05/28/YKaCdO1pexuThbN.gif'
const storeLocation = `${RNFS.TemporaryDirectoryPath}`;
const pathName = `${new Date().getTime()}_babycam_moji.gif`;
const downloadPath = `${storeLocation}${pathName}`;
const ret = RNFS.downloadFile({
  fromUrl: img,
  toFile: downloadPath,
});
ret.promise.then((res) => {
  console.log('res', res);
  if (res && res.statusCode === 200) {
    const promise = CameraRoll.saveToCameraRoll(
      `file://${downloadPath}`,
    );
    promise.then(() => {});
  } else {
  }
});
  1. 首先通过 rn-fetch-blob 下载 GIF
RNFetchBlob.config({appendExt: 'gif', fileCache: true})
  .fetch('GET', img)
  .then((res) => {
  console.log('res', res.path());
  CameraRoll.saveToCameraRoll(res.path());
});

如何以 react-native 将 GIF 保存到 iOS 中的相册?

谢谢

标签: iosreact-nativegif

解决方案


推荐阅读