I want download image from URL and save in the Gallery.
This source works well on Android, but not iOS.
import RNFetchBlob from "rn-fetch-blob";
let imgUrl = 'https://static.scientificamerican.com/sciam/cache/file/EAF12335-B807-4021-9AC95BBA8BEE7C8D_source.jpg'
let newImgUri = imgUrl.lastIndexOf('/');
let imageName = imgUrl.substring(newImgUri);
let dirs = RNFetchBlob.fs.dirs;
let path = Platform.OS === 'ios' ? dirs['MainBundleDir'] + imageName : dirs.PictureDir + imageName;
 RNFetchBlob.config({
     fileCache: true,
     appendExt: 'png',
     indicator: true,
     IOSBackgroundTask: true,
     path: path,
     addAndroidDownloads: {
         useDownloadManager: true,
         notification: true,
         path: path,
         description: 'Image'
      },
     }).fetch("GET", imgUrl).then(res => { 
            console.log(res, 'end downloaded') 
   });
witch one Permissions I needs for iOS, to save Photo in the Gallery?
For iOS you there is no need to use fetch-blob and download it. you need to use CameraRoll from react-native and call saveToCameraRoll()
https://facebook.github.io/react-native/docs/cameraroll
import RNFetchBlob from "rn-fetch-blob";
import { CameraRoll, Platform } from "react-native";
let imgUrl = 'https://static.scientificamerican.com/sciam/cache/file/EAF12335-B807-4021-9AC95BBA8BEE7C8D_source.jpg'
let newImgUri = imgUrl.lastIndexOf('/');
let imageName = imgUrl.substring(newImgUri);
let dirs = RNFetchBlob.fs.dirs;
let path = Platform.OS === 'ios' ? dirs['MainBundleDir'] + imageName : dirs.PictureDir + imageName;
saveToGallery = () => {
  if (Platform.OS == 'android') {
    RNFetchBlob.config({
      fileCache: true,
      appendExt: 'png',
      indicator: true,
      IOSBackgroundTask: true,
      path: path,
      addAndroidDownloads: {
        useDownloadManager: true,
        notification: true,
        path: path,
        description: 'Image'
      },
    }).fetch("GET", imgUrl).then(res => {
      console.log(res, 'end downloaded')
    });
  } else {
    CameraRoll.saveToCameraRoll(imgUrl);
  }
}
You have to use camera roll.
npm install @react-native-community/cameraroll --save
react-native link @react-native-community/cameraroll
from:
import { CameraRoll } from "react-native";
to
import CameraRoll from "@react-native-community/cameraroll";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With