现有文件出现错误 "File ... couldn’t be opened because there is no such file"

Getting error "File ... couldn’t be opened because there is no such file" for exising file

我在 iOS 和 iPhone 6 模拟器上使用 rn-fetch-blob 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文件夹 URL。 这是保存到 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 是一个很好的解决方案。