iPhone 复制文件

iPhone copy file

我用这段代码创建了两个文件路径:

NSString *fileName = [[self genRandStringLength:15] stringByAppendingString:@".mp4"];
NSString *finalFilePath = [self.videoFiles stringByAppendingString:fileName];

NSString *tmpFile = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"temp.mp4"]];
[[NSFileManager defaultManager] removeItemAtPath:tmpFile error:NULL];

然后我有一些代码来创建视频文件,我想将它保存在文档文件夹中:

[[NSFileManager defaultManager] copyItemAtPath:tmpFile toPath:finalFilePath error:&err];

我收到这个错误:

Error Domain=NSCocoaErrorDomain Code=4 "The file “temp.mp4” doesn’t exist." 
UserInfo={NSSourceFilePathErrorKey=/private/var/mobile/Containers/Data/Application/E5B7BBCB-311F-41A3-949D-8A309F535C95/tmp/temp.mp4, NSUserStringVariant=(
    Copy
    ), NSDestinationFilePath=/var/mobile/Containers/Data/Application/E5B7BBCB-311F-41A3-949D-8A309F535C95/Documents/VideoFiles/0iWDmhLvbbzM6SE.mp4, NSFilePath=/private/var/mobile/Containers/Data/Application/E5B7BBCB-311F-41A3-949D-8A309F535C95/tmp/temp.mp4, NSUnderlyingError=0x12f0099b0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

知道问题出在哪里吗?

只是为了一些检查,我尝试将视频复制到 iPhone 照片库: UISaveVideoAtPathToSavedPhotosAlbum(tmpFile, self, nil, nil);

它工作正常,所以文件存在。

使用此代码

NSString *fileName = [[self genRandStringLength:15] stringByAppendingString:@".mp4"];
NSString *finalFilePath = [self.videoFiles stringByAppendingString:fileName];

NSString *tmpFile = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"temp.mp4"]];

NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL success = [fileManager fileExistsAtPath:tmpFile];

if (success)
{
  [[NSFileManager defaultManager] copyItemAtPath:tmpFile toPath:finalFilePath error:&err];

  [[NSFileManager defaultManager] removeItemAtPath:tmpFile error:NULL];

}