reactnative保存图片到相册-CSDN博客
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
安装camera-roll
npm install @react-native-camera-roll/camera-roll --save
判断是本地文件还是网络图片
const isLocalFile = (path) => {
const localFilePrefixes = ['file://', '/'];
for (const prefix of localFilePrefixes) {
if (path.startsWith(prefix)) {
return true;
}
}
return false;
};
保存图片
const saveToCameraRoll = async (imageUrl) => {
setIsLoading(true);
const {config, fs} = RNFetchBlob;
if(isLocalFile(imageUrl)) {
CameraRoll.saveToCameraRoll(imageUrl, 'photo')
.then(() => {
EasyToast.show('图片已成功保存到相册', 1000)
setIsLoading(false)
})
.catch((error) => {
EasyToast.show('图片保存失败', 1000)
setIsLoading(false)
});
return
}
try {
const response = await RNFetchBlob.config({
fileCache: true,
appendExt: 'png',
}).fetch('GET', imageUrl);
const imagePath = response.path();
const result = await CameraRoll.saveToCameraRoll(imagePath);
if (result) {
setIsLoading(false)
EasyToast.show('图片已成功保存到相册', 1000)
} else {
setIsLoading(false)
EasyToast.show('图片保存失败', 1000)
}
} catch (error) {
setIsLoading(false)
}
};
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |