在 iOS 中保存/检索视频文件

发布于 2024-11-07 07:47:00 字数 803 浏览 0 评论 0原文

我一直在浏览但找不到解决我的问题的方法。我正在尝试将一些视频文件保存到我的应用程序的目录中,我按如下方式执行此操作:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"myFile"];
NSLog(@"%@",appFile);

[[NSUserDefaults standardUserDefaults] setValue:appFile forKey:@"videoURL"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
[webData writeToFile:appFile atomically:YES];

我的问题是,这是保存电影文件的正确方法吗?如果是,如何将 NSData 转换为可以播放的文件通过 MPMoviePlayer?感谢您的帮助。

附言。我真的不想使用照片库,因为该应用程序可能包含大量视频。

I've been browsing around but couldn't find a solution to my problem. I'm trying to save some video files to my application's directory I do this as follows:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"myFile"];
NSLog(@"%@",appFile);

[[NSUserDefaults standardUserDefaults] setValue:appFile forKey:@"videoURL"];
[[NSUserDefaults standardUserDefaults]synchronize];
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
[webData writeToFile:appFile atomically:YES];

My question is, is this the proper way of saving the movie file and if yes how do I convert the NSData to a file that can be played back via MPMoviePlayer? Thanks for your help.

PS. I really don't want to use the photo library as the app is likely to hold a fair amount of videos.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

述情 2024-11-14 07:47:00

如果您确实想将其保存在应用程序的文档中,则必须使用 NSFileManager

它可能看起来像这样,

NSString* completeFileName = [NSString stringWithFormat:@"%@",movieName];
NSString* filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:completeFileName];

[[NSFileManager defaultManager] createFileAtPath:filename contents:yourMovieData attributes:nil];

您可能还需要使用 NSFileHandle

要在 MPMoviePlayer 中读取视频,如果您认为可以创建这样的 NSURL

[NSURL fileURLWithPath:path isDirectory:NO];

,那么您将该 url 提供给您的 MPMoviePlayer。

我希望这会对您有所帮助。

If you really want to save it in the documents of your app, you'll have to use NSFileManager

it would probably look like something like that

NSString* completeFileName = [NSString stringWithFormat:@"%@",movieName];
NSString* filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:completeFileName];

[[NSFileManager defaultManager] createFileAtPath:filename contents:yourMovieData attributes:nil];

You also might need to use NSFileHandle

To read the video in your MPMoviePlayer, if think you can make an NSURL like this

[NSURL fileURLWithPath:path isDirectory:NO];

then you give that url to your MPMoviePlayer.

I hope this will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文