0

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

这是我的代码:

-(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

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

4

1 回答 1

2

试试这个代码,当你保存一些东西时,文件应该有一个文件名和该文件的扩展名。在您的代码中,目标保存路径只是文档目录。那是错的。使用 [documentsDir stringByAppendingPathComponent:filename.extension] 将为您提供正确的目标文件路径,[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);
    }
}
于 2013-04-29T07:37:10.053 回答