Image is not showing in the iOS device after getting downloaded through rn-fetch-blob ( React native fetch blob)

不羁岁月 提交于 2019-12-02 20:27:53

问题


I am trying to download a small image using this particular code snippet :

Plug in Used : "rn-fetch-blob": "^0.10.14"

 RNFetchBlob.
config({
  fileCache: true,
 
}).
fetch('GET', url, {      //getting image URL from server
  // some headers ..
})
  .then((response) => {
    const base64Str = response.data;
    RNFetchBlob.fs
          .writeFile(imageLocation, base64Str, 'base64')
          .then(() => {
 
              console.log("Download successful");
           
          }).catch((err) => {
            console.log('Failed to save file. ', err);
          });

variable imageLocation points to ${RNFetchBlob.fs.dirs.DocumentDir}/${filename}.

Also tried with ${RNFetchBlob.fs.dirs.CacheDir}/${filename}.

Download is successful but no image is shown in device photos.


回答1:


On iOS both documents and cache directory are contained within the application's own directory. So you are not storing the image in the device photos.

To download it to the device photos you need to use CameraRoll from react-native https://facebook.github.io/react-native/docs/cameraroll

import { CameraRoll } from 'react-native';

This tutorial shows you how to use CameraRoll

https://medium.com/react-native-training/mastering-the-camera-roll-in-react-native-13b3b1963a2d

However the crux of it is you can use something like this to download images to the camera roll

saveToCameraRoll = (image) => {
  if (Platform.OS === 'android') {
    RNFetchBlob
      .config({
        fileCache : true,
        appendExt : 'jpg'
      })
      .fetch('GET', image.urls.small)
      .then((res) => {
        CameraRoll.saveToCameraRoll(res.path())
          .then(Alert.alert('Success', 'Photo added to camera roll!'))
          .catch(err => console.log('err:', err))
      })
  } else {
      CameraRoll.saveToCameraRoll(image.urls.small)
        .then(Alert.alert('Success', 'Photo added to camera roll!'))
  }
}


来源:https://stackoverflow.com/questions/54123046/image-is-not-showing-in-the-ios-device-after-getting-downloaded-through-rn-fetch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!