从iOS上的url下载图像
我使用react native fs从iOS中的url下载图像。我通过路径检查模拟器的下载成功: /用户/管理员/库/开发者/核心模拟器/设备/443C8F7F-AB92-4217-A999-A7BDBA52A6B6/数据/容器/数据/应用程序/99958A5C-E350-4855-B8CE-ED6FA999C619/文档 在真实设备上,它的路径(iPhone 8 Plus): /var/mobile/Containers/Data/Application/63F84623-E684-456F-A55A-01E22708C012/Documents/pexels-photo-1109561.jpeg 我尝试设置:从iOS上的url下载图像,ios,react-native,Ios,React Native,我使用react native fs从iOS中的url下载图像。我通过路径检查模拟器的下载成功: /用户/管理员/库/开发者/核心模拟器/设备/443C8F7F-AB92-4217-A999-A7BDBA52A6B6/数据/容器/数据/应用程序/99958A5C-E350-4855-B8CE-ED6FA999C619/文档 在真实设备上,它的路径(iPhone 8 Plus): /var/mobile/Containers/Data/Application/63F84623-E684-456F-
const destPath=RNFS.DocumentDirectoryPath+'/images/'+name代码>
因为当我在真实设备上从照片中拾取图像时,这是路径:
file:///var/mobile/Containers/Data/Application/8DE01885-DEFE-4667-B6FF-CE693A0DADB3/Documents/images/1520FCA5-80AB-4300-A7F6-2D2C974D726A.jpg
但它不起作用。我想在照片上显示这个图像,这意味着下载成功后,我可以立即在我的照片上看到它,如何做到这一点,以及其他文件,如:mp3、mp4、文本,下载后,如何找到它,并在用户想要阅读时向其显示
这是我的代码:
downloadImage = () => {
// download(this.props.image.url, '_Subiz/image_' + this.props.image.name);
this.download(this.props.image.url, `${RNFS.DocumentDirectoryPath}/react-native.png`)
};
download = async (target, destination) => {
try{
let options = {
fromUrl: target,
toFile: destination,
begin: (res) => {
console.log(res)
},
progress: (data) => {
console.log(data)
},
background: true,
progressDivider: 1
};
console.log(options);
const request = await RNFS.downloadFile(options).promise
console.log(request)
}catch(e){
console.log(e)
}
};
如果你使用图书馆。你可以用简单的方式下载你的图片。你们也可以在照片库中看到它
const { fs } = RNFetchBlob;
let Picture = fs.dirs.PictureDir;
RNFetchBlob
.config({
appendExt : 'png',
fileCache: true,
path: Picture+(new Date()).getTime()+'.png',
})
.fetch('GET', 'image_url')
.then((resp) => {
// the image path you can use it directly with Image component
console.log(resp.path());
return resp.readFile('base64'); // If you get base64 file
})
.then((base64Data) => {
console.log(base64Data);
}).catch((errorMessage, statusCode) => {
// error handling
console.log('errorMessage2:'+errorMessage)
});
我试过了,但没用。我在iPhone上看不到库中的图像。这是下载完成后的路径:========================================>',“/var/mobile/Containers/Data/Application/9481E826-47FF-4BF0-B0E3-784161F01F00/Documents/RNFetchBlob\u tmp/RNFetchBlobTmp\u 9mqxc3swc17k85gnnva53”
检查我的更新答案谢谢,以获取答案,但图片未定义
且不起作用请确定让图片=fs.dirs.PictureDir;你能把它记录到console.log(fs.dirs.PictureDir);//日志是什么?