ios - 获取错误 "File ... couldn’ t be opened because there is no such file"for existing file

标签 ios react-native react-native-fetch-blob

我正在使用 rn-fetch-blob带有 iPhone 6 模拟器的 iOS 上的 0.10.8 和 RN 0.49。

我正在使用 react-native-image-resizer 模块调整图像大小,然后使用 react-native-fetch-blob 将该图像移动到正确的位置。但是,这样做时出现错误:

The file “6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg” couldn’t be opened because there is no such file.

在进行复制之前,我将源和目标打印到控制台:

"Moving file:///Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3-8D8F-69207A8C9454/Library/Caches/6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg => /Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3-8D8F-69207A8C9454/Documents/testing.jpg"

我可以验证路径 //Users/laurent/Library/Developer/CoreSimulator/Devices/3AF6C788-B6ED-41DD-85F0-32D719DB0DBE/data/Containers/Data/Application/A401D341-9C60-4AA3- 8D8F-69207A8C9454/Library/Caches/6492238E-AAAC-4DC6-90F3-AFAB7225DBD5.jpg 存在,因为我可以在 Finder 中打开它。但是由于某种原因 rn-fetch-blob 没有找到它。

知道可能是什么问题吗?

有关我正在使用的代码的信息:

const resizedImage = await ImageResizer.createResizedImage(localFilePath, dimensions.width, dimensions.height, format, 85);
const resizedImagePath = resizedImage.uri;
console.info('Moving ' + resizedImagePath + ' => ' + targetPath);
await RNFetchBlob.fs.cp(resizedImagePath, targetPath); // Throws error

最佳答案

问题是 JavaScript 无法访问此路径,您需要先将其保存到有效位置,例如应用程序 temp 文件夹,然后传递 temp文件夹网址。 这是在 iOS 上保存到 temp 文件夹的方式:

+(NSURL*)saveToTmpFolder:(NSData*)data {
    NSString *temporaryFileName = [NSProcessInfo processInfo].globallyUniqueString;
    NSString *temporaryFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[temporaryFileName stringByAppendingPathExtension:@"jpg"]];
    NSURL *temporaryFileURL = [NSURL fileURLWithPath:temporaryFilePath];

    NSError *error = nil;
    [data writeToURL:temporaryFileURL options:NSDataWritingAtomic error:&error];

    if ( error ) {
        //NSLog( @"Error occured while writing image data to a temporary file: %@", error );
    }
    else {
        //NSLog(@"Image Saved - YOU ROCK!");
    }
    return temporaryFileURL;
}

或者,如果您需要 JavaScript API,react-native-fs是一个很好的解决方案。

关于ios - 获取错误 "File ... couldn’ t be opened because there is no such file"for existing file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47381151/

相关文章:

iOS:将 ObjC 代码转换为 C#,如何知道应用程序处于空闲状态的时间

javascript - react native 表单请求

ios - ld : library not found for -lreact-native-fetch-blob with pods?

React-native-android - 如何将图像保存到 Android 文件系统并在手机的 'Gallery' 中查看

javascript - 无法读取未定义的 React-Native Firebase React-native-fetch-blob 的属性 'DocumentDir'

ios - swift GMSPlacePicker 不显示 map

python - 处理来自 IOS 设备的请求时发生异常

react-native - react-navigation TabNavigator 仅呈现第一个选项卡

react-native - 使用 react-native-fbsdk 注销的正确方法

ios - 如何在 Swift3 iOS 中的特定持续时间内在 AVPlayer 中播放视频