3

In my react native app i am showing several images in <Image> tags and i load those images from local folder.

<Image source={require('./src/images/image1.jpg')} />

I want to save image when the user tapped on it. I can get which image user tapped and pass it to the function. But i put a single image path to it.

_onPressButton(imgName) {
  CameraRoll.saveToCameraRoll( './src/images/image1.jpg' , 'photo').then(function(result) {
    alert(result);
  }).catch(function(error) {
    alert(error);
  });
}

But this gives me an error in iOS emulator saying it cant find the image in the path.

When i give as,

CameraRoll.saveToCameraRoll( 'https://i.imgur.com/JnrwMpZ.jpg' , 'photo')

It works.

But i want to save these files in my src/images folder. How can i get a path of this image file OR get this done..?

I appreciate any help regarding this.

Thanks

Rakesh Patel
  • 1,673
  • 10
  • 27
Janith Ganewatta
  • 143
  • 1
  • 4
  • 12

1 Answers1

3

Here is my solution with Expo, worked in android and should work on ios too

onSave = async () => {
  const asset = Asset.fromModule(require('./src/images/image1.jpg'))
  if (!asset.localUri) {
    await asset.downloadAsync();
  }
  const uri = asset.localUri;
  CameraRoll.saveToCameraRoll(uri, 'photo')
}
izht
  • 130
  • 1
  • 11
  • Thanks for your answer. Another developer of our company had fixed the issue with other library. I will try this when I get a chance and will let you know. – Janith Ganewatta Aug 28 '18 at 04:54
  • 2
    @JanithGanewatta Ganewatta Maybe you will post the answer? – rendom Feb 08 '19 at 05:21
  • i got this error ***The method or property expo-file-system.downloadAsync is not available on ios, are you sure you've linked all the native dependencies properly?*** – famfamfam Aug 25 '22 at 10:20