iPhone复制文件

时间:2015-10-06 21:47:36

标签: ios objective-c nsfilemanager

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

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);

它工作正常,因此文件存在。

1 个答案:

答案 0 :(得分:1)

使用此代码

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];

}