【问题标题】:Save recorded video in documents folder将录制的视频保存在文档文件夹中
【发布时间】:2013-04-29 07:28:15
【问题描述】:

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

这是我的代码:

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

有人知道为什么它不起作用吗?

【问题讨论】:

  • 检查我的答案,将视频保存到我已更新的目录中。

标签: iphone xcode


【解决方案1】:

试试这个代码,当你保存一些东西时,文件应该有一个文件名和该文件的扩展名。在您的代码中,目标保存路径只是文档目录。那是错的。使用 [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);
    }
}

【讨论】:

  • 我在答案中添加了几行,通过它们。如果您需要更多解释,请告诉我,希望您将其标记为答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-05
  • 2021-10-19
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
相关资源
最近更新 更多