将录制的视频保存在文档文件夹

时间:2013-04-29 07:28:15

标签: iphone xcode

我正在尝试在录制后立即将电影保存在文档文件夹中。

这是我的代码:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {



    NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
    [self dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"path from image picker: %@", moviePath);

    NSFileManager* fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString* documentsDir = [paths objectAtIndex:0];

    NSLog(@"%@",documentsDir);

    [fileManager moveItemAtPath:moviePath toPath:documentsDir error:nil];

    NSArray *files = [fileManager contentsOfDirectoryAtPath:documentsDir
                                                  error:nil];

    for (id object in files) {
        NSLog(@"%@",object);
    }
}

正如您可能猜到的那样,它不起作用。

第一个日志说:来自图像选择器的路径:/private/var/mobile/Applications/6630FBD3-1212-4ED0-BC3B-0C23AEEFB267/tmp/capture-T0x1e575330.tmp.SQQ2C5/capturedvideo.MOV

秒日志说:/ var / mobile / Applications / 6630FBD3-1212-4ED0-BC3B-0C23AEEFB267 / Documents

任何人都知道它为什么不起作用?

1 个答案:

答案 0 :(得分:2)

尝试此代码,当您保存某些内容时,该文件应具有该文件的文件名和扩展名。在您的代码中,目标保存路径只是文档目录。那是错的。使用[documentsDir stringByAppendingPathComponent:filename.extension]将为您提供正确的目标filePath,[moviePath lastPathComponent]返回所选文件的文件名+扩展名。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
    [self dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"path from image picker: %@", moviePath);

    NSFileManager* fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString* documentsDir = [paths objectAtIndex:0];
    NSString *savePath = [documentsDir stringByAppendingPathComponent:[moviePath lastPathComponent]];

    NSLog(@"%@",savePath);

    [fileManager moveItemAtPath:moviePath toPath:savePath error:nil];

    NSArray *files = [fileManager contentsOfDirectoryAtPath:documentsDir
                                                      error:nil];

    for (id object in files) {
        NSLog(@"%@",object);
    }
}