React Native 从 url 下载图像并保存在图库中
React Native Download image from url and save in Gallery
我想从 URL 下载图片并保存在图库中。
此来源适用于 Android,但不适用于 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')
});
我需要 iOS 的权限才能将照片保存在图库中?
对于iOS,您无需使用 fetch-blob 并下载它。
您需要使用来自 react-native 的 CameraRoll 并调用 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);
}
}
你必须使用相机胶卷。
npm install @react-native-community/cameraroll --save
react-native link @react-native-community/cameraroll
来自:
从 "react-native" 导入 { CameraRoll};
至
从“@react-native-community/cameraroll”导入相机胶卷;
我想从 URL 下载图片并保存在图库中。
此来源适用于 Android,但不适用于 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')
});
我需要 iOS 的权限才能将照片保存在图库中?
对于iOS,您无需使用 fetch-blob 并下载它。 您需要使用来自 react-native 的 CameraRoll 并调用 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);
}
}
你必须使用相机胶卷。
npm install @react-native-community/cameraroll --save
react-native link @react-native-community/cameraroll
来自:
从 "react-native" 导入 { CameraRoll};
至
从“@react-native-community/cameraroll”导入相机胶卷;