如何将视频文件保存到文档目录中
我正在使用以下代码捕获视频:
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.delegate = self;
//need to handle delegates methods
//
ipc.allowsEditing = YES;
ipc.videoQuality = UIImagePickerControllerQualityTypeMedium;
ipc.videoMaximumDuration = 30.0f; // 30 seconds
//temporary duation of 30 seconds for testing
ipc.mediaTypes = [NSArray arrayWithObject:@"public.movie"];
// ipc.mediaTypes = [NSArray arrayWithObjects:@"public.movie", @"public.image", nil];
[self presentModalViewController:ipc animated:YES];
//this controller allows to record the videos
我可以使用以下代码将录制的视频保存到相册,
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// recover video URL
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
// check if video is compatible with album
BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([url path]);
// save
if (compatible){
UISaveVideoAtPathToSavedPhotosAlbum([url path], self, @selector(video:didFinishSavingWithError:contextInfo:), NULL);
NSLog(@"saved!!!! %@",[url path]);
}
[self dismissModalViewControllerAnimated:YES];
[picker release];
}
但我需要从相册中检索该文件并需要存储到文档目录中?
I am capturing video using following code:
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.delegate = self;
//need to handle delegates methods
//
ipc.allowsEditing = YES;
ipc.videoQuality = UIImagePickerControllerQualityTypeMedium;
ipc.videoMaximumDuration = 30.0f; // 30 seconds
//temporary duation of 30 seconds for testing
ipc.mediaTypes = [NSArray arrayWithObject:@"public.movie"];
// ipc.mediaTypes = [NSArray arrayWithObjects:@"public.movie", @"public.image", nil];
[self presentModalViewController:ipc animated:YES];
//this controller allows to record the videos
and I can save recorded video to album using following code
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// recover video URL
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
// check if video is compatible with album
BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([url path]);
// save
if (compatible){
UISaveVideoAtPathToSavedPhotosAlbum([url path], self, @selector(video:didFinishSavingWithError:contextInfo:), NULL);
NSLog(@"saved!!!! %@",[url path]);
}
[self dismissModalViewControllerAnimated:YES];
[picker release];
}
but I need to retrieve that file from album and need to store into document directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
将视频保存到documents目录下如下:
Saving the video to the documents directory is as follows:
如果将来有人需要的话,这是快速代码:
Here is the swift code if anyone need it in future:
一点研发,这对我有用
{
}
//此外,当您必须检查应用程序文档目录中是否已存在相同视频并且您不想创建它的多个副本时,请进行一些更改,如下所示
}
//其中weakSelf.selectedFileName & weakSelf.filePathURL 分别是我的类的 NSString 和 NSURL 类型属性。
Bit of R&D, this worked for me
{
}
//Also when you have to check for same video already exist in app document directory and you don't want create multiple copies of it then made some changes as below
}
//Where weakSelf.selectedFileName & weakSelf.filePathURL are NSString and NSURL type properties of my class respectively.
我们可以使用 NSFileManager 的 moveItemAtPath:toPath:error: 方法将视频文件移动到我们应用程序的文档目录。效率更高。
此外,我们应该获取视频文件的扩展名并将其用作本地视频文件名的扩展名。
这对我有用。
We can use NSFileManager's
moveItemAtPath:toPath:error:
method to move the video file to our app's document directory. It's more efficient.Also we should get the extension of the video file and use it as extension of our local video file name.
It works for me.
只需调用此函数,它就会为您完成所有操作:)
Just Call this function, and it will do every thing for you :)
希望这有帮助
Hope this help
迅捷 3/4
保存示例路径:-
Swift 3/4
saving example Path:-