Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to Save an Image #185

Open
L-U-C-K-Y opened this issue Apr 4, 2023 · 1 comment
Open

Possibility to Save an Image #185

L-U-C-K-Y opened this issue Apr 4, 2023 · 1 comment

Comments

@L-U-C-K-Y
Copy link

Hi all

Thanks for this library!

I was wondering how to save an image to the photo gallery.
I saw that there is a onLongPress prop that we could set.

Are there any recommendations on how to save it and ask for permissions in the next step?

Thank you

@thongle12
Copy link

thongle12 commented Aug 1, 2023

IDK the package support it?

My solution is

import React, { useState } from 'react';
import { View, Image, TouchableOpacity, Text, StyleSheet } from 'react-native';
import ImageView from "react-native-image-viewing";
import RNFetchBlob from 'rn-fetch-blob';

const YourComponent = () => {
const imageUrl = 'URL_OF_YOUR_IMAGE';
const [isImageViewVisible, setImageViewVisible] = useState(false);

const handleDownloadImage = async () => {
try {
const res = await RNFetchBlob.config({
fileCache: true,
appendExt: 'png',
}).fetch('GET', imageUrl);

  const savedImagePath = res.path();
  console.log('Image downloaded to:', savedImagePath);
} catch (error) {
  console.error('Error downloading image:', error);
}

};

return (

<TouchableOpacity onPress={() => setImageViewVisible(true)}>
<Image source={{ uri: imageUrl }} style={styles.image} />

  <ImageView
    images={[{ uri: imageUrl }]}
    imageIndex={0}
    visible={isImageViewVisible}
    onRequestClose={() => setImageViewVisible(false)}
    FooterComponent={({ imageIndex }) => (
      <TouchableOpacity onPress={handleDownloadImage}>
        <Text style={styles.downloadButton}>Download</Text>
      </TouchableOpacity>
    )}
  />
</View>

);
};

export default YourComponent;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants